csells
csells

Reputation: 2593

how do I get the class/property/etc associated with a .NET attribute?

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.

Upvotes: 2

Views: 479

Answers (2)

kervin
kervin

Reputation: 11858

You have to pass it in. See this answer...

How do I get the member to which my custom attribute was applied?

Upvotes: 2

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

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.

Upvotes: 4

Related Questions