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.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
csells
  • 2,453
  • 1
  • 20
  • 18

2 Answers2

4

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
  • 67,283
  • 14
  • 105
  • 142
2

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

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

Community
  • 1
  • 1
kervin
  • 11,672
  • 5
  • 42
  • 59