Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.60 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to retrieve an attribute value along with a runtime property value
  2. public class MyClass
  3. {
  4.    [Example("MyClassID")]
  5.    public string ID;
  6. }
  7.        
  8. MyClass mc = new MyClass();
  9. mc.ID = 18;
  10.        
  11. mc.ID.GetCustomAttributes(typeof(ExampleAttribute), false)
  12.        
  13. this.GetType().GetProperty("ID").GetCustomAttributes(typeof(ExampleAttribute))
  14.        
  15. Type t = this.GetType();
  16.  var id = t.GetProperty("ID");
  17.  var attribute = id.GetCustomAttributes(typeof(ExampleAttribute), false).FirstOrDefault();
  18.  if (attribute != null)
  19.  {
  20.        ExampleAttribute item = attribute as ExampleAttribute;
  21.        //Do stuff with the attribute
  22.  }