Wednesday, August 27, 2008

Extension Types for easier (and strongly typed) Attribute checks

I just saw a blog post from Alex Mueller about marker interfaces and C# attributes and which way you should be using them.

I, for one, believe that attributes are far the best way to mark a class instead of using an empty marker interface. Why? Well, there are several good reasons to use attributes over interfaces, especially when it comes to inheritance. That aside, attributes are also a lot more flexible than interfaces.

So why don’t people use attributes more often? The most common complaint is that interfaces are easier to use and check for in code (I.e. interfaces requires less code). But I think this is mainly because people don’t know where to look for help in the framework. So I’ve devised 30 extension methods that will make your work with attributes a little bit easier and strongly typed.

Now you can check for the existence of an attribute like this:

Well, I hope someone can use this… Any thoughts or criticisms for the code will be greatly appreciated. Otherwise, enjoy! ;)

6 comments:

  1. Nice class! Have you considered or tested the performance difference of the marker interface check vs checking for attributes using your code?
    ReplyDelete
  2. Performance of attributes vs. interfaces is very different and attribute comes out losing. In the tests I did, checking attributes were significantly slower than checking interfaces, sometimes up to 1000-2000 times slower!

    But don't be too scared. For individual checks we are still speaking about 0.00002 secs. per attribute check, so it is not hurting your performance in any noticeable way as long as you aren't doing several thousand checks per second.

    The thing to do is to judge it on a case by case basis and weigh the pros and cons whenever you need to employ a marker interface or attribute. Interfaces may be fast but has less flexibility than attributes.
    ReplyDelete
  3. And with that, I believe you've addressed the two most significant concerns of the subject matter. Thank you!
    ReplyDelete
  4. You may use this mixin :)

    public static bool IsAttributeDefined<T>(this ICustomAttributeProvider provider, bool inherit = true)
    where T : Attribute
    {
    return provider.IsDefined(typeof(T), inherit);
    }

    public static T[] GetAttributes<T>(this ICustomAttributeProvider provider, bool inherit = true)
    where T : Attribute
    {
    return provider.GetCustomAttributes(typeof(T), inherit) as T[];
    }

    public static T GetFirstAttribute<T>(this ICustomAttributeProvider provider, inherit = true)
    where T : Attribute
    {
    return provider.GetAttributes<T>(inherit).FirstOrDefault();
    }
    }

    P.S. some C# 4.0 code :)
    ReplyDelete
  5. THANKS MAN!, had been working on a litle project like this to make attributeExt, but my brain like poped out :), now this , Good Job!
    ReplyDelete
  6. Thanks for sharing.
    It would be nice if C# allowed defining extension methods based on attributes. That way marker interfaces will be less needed.
    ReplyDelete