views:

49

answers:

2

If I apply a custom attribute to a class, for example:

[Foo]
class Bar {}

It's clear that when I retrieve my Foo attribute instance, that it's associated with a Bar. Inside the Foo implementation, say in the ctor, how do I get the class associated with the instance of the attribute? So far, all I've been able to come up with is putting it into the ctor of the attribute:

[Foo(typeof(Bar)]
class Bar {}

which seems horribly redundant.

+3  A: 

When you are actually using Foo, you should have the class Foo is an attribute of. So if you need to access a property in Foo, you can pass the Type/instance in as an argument.

Yuriy Faktorovich
+1. In the chicken+egg, the attribute is the chicken. You've already got the Bar egg, there is no other way to find the attribute.
Hans Passant