Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# interface question

Tags:

c#

If I want a bunch of classes to implement a method, I can just make them implement an interface. However, if I want the method to always be decorated with two custom attributes, if there a syntax for that? In other words, I want every class that implement method Run() to attach a descriptionAttribute and a versionAttribute.

Update: Is there a way to make classes that implement Run() generate a compile error if they did not attach the two attributes?

like image 616
russ Avatar asked Aug 19 '10 14:08

russ


2 Answers

public interface IRun
{
    void Run();
}

public abstract class RunBase : IRun
{
    [Description("Run Run Run")]
    [Version("1.0")]
    public abstract void Run();
}

public abstract class SoRunning : RunBase
{
    public override void Run() {} 
}

you should be able to get the Attributes off of the base class

like image 154
hunter Avatar answered Oct 22 '22 05:10

hunter


There is no compile time way to enforce that.

like image 43
Yuriy Faktorovich Avatar answered Oct 22 '22 03:10

Yuriy Faktorovich