evolon
evolon

Reputation: 1596

How to read property attribute in the defining type class?

Let's say I have a property defined like below:

[SomeAttriubte("#1")]
public SomeClass Property1 { get; set; }

[SomeAttribute("#2")]
public SomeClass Property2 { get; set; }

Which SomeClass definition is something like this:

public class SomeClass
{
     private void PrivateMethod()
     {
           //Some action
     }
}

Is there any way to read SomeAttribute argument value in the PrivateMethod inside the defining type class??

Upvotes: 0

Views: 102

Answers (1)

xanatos
xanatos

Reputation: 111940

No. The attributes you use are connected to the property. To be able to access them, SomeClass.PrivateMethod() would need to know the PropertyInfo of the properties it is used in. But it can't. To be more "clear": a type doesn't know "where" it is used, and not knowing "where" it is used, it can't access attributes connected to the place "where" it is used (in the same way that it can't access the this of the object "where" it is used)

Upvotes: 2

Related Questions