This line is the problem:
object[] attrs = typeof(T).GetCustomAttributes(true);
You're calling GetCustomAttributes
on Type
- not on the type that you're actually interested in. You want:
object[] attrs = type.GetCustomAttributes(true);
Or rather more simply, replace the rest of the method with:
return (T) type.GetCustomAttributes(typeof(T), true).FirstOrDefault();
Note that that will also pick up attributes which are subclasses of T
, but I'd expect that to be more useful anyway, in the rare cases where you're actually using inheritance on attributes.