2

I have a property which has an attribute applied to it. How can i access the attribute from inside the property getter or setter?

class Person { [ID("A","B")] public Address HomeAddress { get { // Get values A and B here ? } set { } } } 

Really no idea how to go about this.

Regards

3
  • stackoverflow.com/questions/4689361/… see this question Commented Sep 4, 2012 at 18:07
  • what are you trying to achieve? Commented Sep 4, 2012 at 18:10
  • I am attempting to create a simple ORM and want to lazy load a complex property. In winrt there is no reflection.emit so best idea I could come up with was creating a base entity and mapping a foreign key to a local key on the property (A and B). I wanted to try to try make it clean so I dont have to write loads of code in each complex type, instead I wanted a more generic version. Commented Sep 4, 2012 at 18:23

2 Answers 2

2
class Person { [ID("A","B")] public Address HomeAddress { get { System.Attribute[] attrs = System.Attribute.GetCustomAttributes(Person); // use attrs[0] to get "A"; // use attrs[1] to get "B"; } set { } } } 

Ps.: I'm not using Visual Studio right now, just wrote directly here. Sorry if you find any minor error.


Give a look at this MSDN article.

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(Person); // Reflection. // Displaying output. foreach (System.Attribute attr in attrs) { System.Console.WriteLine(attr); } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can try with this code

PropertyInfo[] propertyInfo = type.GetProperties(); foreach (var m in propertyInfo ) { foreach (Attribute a in m.GetCustomAttributes(true)) { } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.