SRReflection.cs 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. namespace SRF.Helpers
  2. {
  3. using System;
  4. using System.Reflection;
  5. public static class SRReflection
  6. {
  7. public static void SetPropertyValue(object obj, PropertyInfo p, object value)
  8. {
  9. #if NETFX_CORE
  10. p.SetValue(obj, value, null);
  11. #else
  12. p.GetSetMethod().Invoke(obj, new[] {value});
  13. #endif
  14. }
  15. public static object GetPropertyValue(object obj, PropertyInfo p)
  16. {
  17. #if NETFX_CORE
  18. return p.GetValue(obj, null);
  19. #else
  20. return p.GetGetMethod().Invoke(obj, null);
  21. #endif
  22. }
  23. public static T GetAttribute<T>(MemberInfo t) where T : Attribute
  24. {
  25. #if !NETFX_CORE
  26. return Attribute.GetCustomAttribute(t, typeof (T)) as T;
  27. #else
  28. return t.GetCustomAttribute(typeof (T), true) as T;
  29. #endif
  30. }
  31. #if NETFX_CORE
  32. public static T GetAttribute<T>(Type t) where T : Attribute
  33. {
  34. return GetAttribute<T>(t.GetTypeInfo());
  35. }
  36. #endif
  37. }
  38. }