7

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?

1
  • you shouldn't need to worry about compile errors since those attributes will always be there in the base class Commented Aug 19, 2010 at 18:36

5 Answers 5

6
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

Sign up to request clarification or add additional context in comments.

7 Comments

If you have an abstract class, why do you need the interface too?
@Juliet -- doing it this way won't impact the rest of your code, assuming you're programming to the interface IRun.
You should remove the {} and replace with ; in the abstract run method in RunBase
+1, This is a good solution but not foolproof. The derived class can add new versions of the attributes with different incorrect values.
I'm assuming that the attribute values will be different for each implementation. If that assumtpation is right this solution has no benifit.
|
3

There is no compile time way to enforce that.

Comments

3

Maybe you should consider using abstract classes ? http://msdn.microsoft.com/en-us/library/k535acbf%28VS.71%29.aspx

(Not sure I understood your question well)

2 Comments

@hunter: They're both .NET so I think that example could be easily ported to C# (probably nothing more than syntax/keyword changes).
right, I just noticed asker had 16 rep and the link went to VB so maybe he wouldn't be able to port it him/herself
1

I don't think there is built in compile time support. However one way you could get around this is to create a program or script that is ran at compile time by using the projects build events.

For example:

  1. Create a small program that through the commandline will take in a file path to an assembly.
  2. Make the program load the assembly from the command line
  3. Get all types out of the assembly
  4. Verify that all of the types that implement your interface have the attributes set
  5. Have the program throw an exception if conditions are not met otherwise it runs successfully
  6. Add the program to the post-build event commands through the project properties

Note: This could also be done as a 'unit' test.

Comments

0

You could use an abstract class to validate the attributes when the class is initialized.

abstract class MustImpliment
{
    protected MustImpliment()
    {
        object[] attributes = this.GetType().GetCustomAttributes(typeof(DescriptionAttribute), true);

        if (attributes == null || attributes.Length == 0)
            throw new NotImplementedException("Pick a better exception");
    }

    // Must impliment DescriptionAttribute
    public abstract void DoSomething();
}

class DidntImpliment : MustImpliment
{
    public override void DoSomething()
    {
        // ...
    }
}

Update: This will not cause a compiler error, but you can use this as part of a Unit Test as Jerod Houghtelling suggested.

1 Comment

One problem I see with this is the multiple inheritance issue. If the class is already inheriting from an object then it might not be appropriate to insert the MustImpliment into the chain whereever that location might be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.