
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 0.60 KB | hits: 15 | expires: Never
How to retrieve an attribute value along with a runtime property value
public class MyClass
{
[Example("MyClassID")]
public string ID;
}
MyClass mc = new MyClass();
mc.ID = 18;
mc.ID.GetCustomAttributes(typeof(ExampleAttribute), false)
this.GetType().GetProperty("ID").GetCustomAttributes(typeof(ExampleAttribute))
Type t = this.GetType();
var id = t.GetProperty("ID");
var attribute = id.GetCustomAttributes(typeof(ExampleAttribute), false).FirstOrDefault();
if (attribute != null)
{
ExampleAttribute item = attribute as ExampleAttribute;
//Do stuff with the attribute
}