views:

177

answers:

2

I have an attribute lets call it SomeAttribute and a class i need to make sure the class is passed a type which has SomeAttribute. So this is how i do it now:

public class Test()
{
    public Test(SomeType obj)
    {
        if(!obj.GetType().IsDefined(typeof(SomeAttribute), false))
        {
            throw new ArgumentException("Errormessage");
        }
    }
}

But this means that i don't get any errors at compile time but somewhere at runtime, if obj does not have the attribute. Is there a way to specify in the method declaration that the parameter must have some attribute ? So i get errors i compile time when using the wrong parameters, or do i have to use an empty interface ?

+1  A: 

No, there's no way of requiring an attribute - but you could always write unit tests to scan through your assemblies and check that the appropriate attributes had been applied - assuming you can actually detect this requirement.

It sounds somewhat odd though - could you give some more details as to why you're using this scheme? Perhaps another approach would be more appropriate, such as another parameter to describe the extra data you're interested in.

Jon Skeet
Actually I wonder if there's some way of doing this using code contracts? I have very little experience with them but it does allow compile-time checks of otherwise runtime-only conditions.
Josh Einstein
I want to use it to mark objects so i am sure they are of some type, and i read that using empty interfaces is bad practice so i did it with attributes. The type doesn't need to implement anything.
slayerIQ
I don't think empty interfaces are bad practice, they're the perfect solution for this. However, what does "of some type" really mean? If the object doesn't have to implement anything (just be marked as something), what is your method going to do with it?
Matti Virkkunen
I have a collection which stores objects based on their types, and i can filter types from that collection with a predicate.
slayerIQ
@slayerIQ: What do you mean by "based on their types"? What does the attribute signify? Could you make your class generic, with a type constraint perhaps?
Jon Skeet
Yes i could but wouldn't the type i constrain it to be the empty interface ?
slayerIQ
The point Jon is making is that an empty interface is not conveying a whole lot of information. You can't access any member on the parameter unless it's defined in it's interface or you do some typecasting that is considered bad practice.
Tigraine
I think i'll go with the attributes with unit testing.
slayerIQ
+3  A: 

There's no way to enforce an attribute at compile-time. You may want to reconsider your design - usually attributes are more about metadata, less about runtime behavior.

NDepend's Code Query Language is a static anlysis tool that could probably detect violations - but if you're not already using NDepend, I'd be hesitant to include it for this requirement alone.

Then again, lacking context of what you want to do - it's a bit like solving the other half your problem.

Mark Brackett