code_junkie

Do auto-implemented properties support attributes

Nov 13th, 2011
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Reflection;
  4.  
  5. class Test
  6. {
  7. [Description("Auto-implemented property")]
  8. public static string Foo { get; set; }
  9.  
  10. static void Main(string[] args)
  11. {
  12. var property = typeof(Test).GetProperty("Foo");
  13. var attributes = property.GetCustomAttributes
  14. (typeof(DescriptionAttribute), false);
  15.  
  16. foreach (DescriptionAttribute description in attributes)
  17. {
  18. Console.WriteLine(description.Description);
  19. }
  20. }
  21. }
  22.  
  23. [Serializable]
  24. public class MyClass
  25. {
  26. [field:NonSerializedAttribute()]
  27. public int Id
  28. {
  29. get;
  30. private set;
  31. }
  32. }
Add Comment
Please, Sign In to add comment