ReflectionExtensions.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace ServiceStack
  7. {
  8. public static class ReflectionExtensions
  9. {
  10. public static Type FirstGenericType(this Type type)
  11. {
  12. while (type != null)
  13. {
  14. if (type.IsGeneric())
  15. return type;
  16. type = type.BaseType();
  17. }
  18. return null;
  19. }
  20. public static Type GetTypeWithGenericTypeDefinitionOf(this Type type, Type genericTypeDefinition)
  21. {
  22. foreach (var t in type.GetTypeInterfaces())
  23. {
  24. if (t.IsGeneric() && t.GetGenericTypeDefinition() == genericTypeDefinition)
  25. {
  26. return t;
  27. }
  28. }
  29. var genericType = type.FirstGenericType();
  30. if (genericType != null && genericType.GetGenericTypeDefinition() == genericTypeDefinition)
  31. {
  32. return genericType;
  33. }
  34. return null;
  35. }
  36. public static PropertyInfo[] GetPublicProperties(this Type type)
  37. {
  38. if (type.IsInterface())
  39. {
  40. var propertyInfos = new List<PropertyInfo>();
  41. var considered = new List<Type>();
  42. var queue = new Queue<Type>();
  43. considered.Add(type);
  44. queue.Enqueue(type);
  45. while (queue.Count > 0)
  46. {
  47. var subType = queue.Dequeue();
  48. foreach (var subInterface in subType.GetTypeInterfaces())
  49. {
  50. if (considered.Contains(subInterface)) continue;
  51. considered.Add(subInterface);
  52. queue.Enqueue(subInterface);
  53. }
  54. var typeProperties = subType.GetTypesPublicProperties();
  55. var newPropertyInfos = typeProperties
  56. .Where(x => !propertyInfos.Contains(x));
  57. propertyInfos.InsertRange(0, newPropertyInfos);
  58. }
  59. return propertyInfos.ToArray();
  60. }
  61. return type.GetTypesPublicProperties()
  62. .Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
  63. .ToArray();
  64. }
  65. public const string DataMember = "DataMemberAttribute";
  66. internal static string[] IgnoreAttributesNamed = new[] {
  67. "IgnoreDataMemberAttribute",
  68. "JsonIgnoreAttribute"
  69. };
  70. public static PropertyInfo[] GetSerializableProperties(this Type type)
  71. {
  72. var properties = type.GetPublicProperties();
  73. return properties.OnlySerializableProperties(type);
  74. }
  75. private static List<Type> _excludeTypes = new List<Type> { typeof(Stream) };
  76. public static PropertyInfo[] OnlySerializableProperties(this PropertyInfo[] properties, Type type = null)
  77. {
  78. var readableProperties = properties.Where(x => x.PropertyGetMethod(nonPublic: false) != null);
  79. // else return those properties that are not decorated with IgnoreDataMember
  80. return readableProperties
  81. .Where(prop => prop.AllAttributes()
  82. .All(attr =>
  83. {
  84. var name = attr.GetType().Name;
  85. return !IgnoreAttributesNamed.Contains(name);
  86. }))
  87. .Where(prop => !_excludeTypes.Contains(prop.PropertyType))
  88. .ToArray();
  89. }
  90. }
  91. public static class PlatformExtensions //Because WinRT is a POS
  92. {
  93. public static bool IsInterface(this Type type)
  94. {
  95. return type.GetTypeInfo().IsInterface;
  96. }
  97. public static bool IsGeneric(this Type type)
  98. {
  99. return type.GetTypeInfo().IsGenericType;
  100. }
  101. public static Type BaseType(this Type type)
  102. {
  103. return type.GetTypeInfo().BaseType;
  104. }
  105. public static Type[] GetTypeInterfaces(this Type type)
  106. {
  107. return type.GetTypeInfo().ImplementedInterfaces.ToArray();
  108. }
  109. internal static PropertyInfo[] GetTypesPublicProperties(this Type subType)
  110. {
  111. var pis = new List<PropertyInfo>();
  112. foreach (var pi in subType.GetRuntimeProperties())
  113. {
  114. var mi = pi.GetMethod ?? pi.SetMethod;
  115. if (mi != null && mi.IsStatic) continue;
  116. pis.Add(pi);
  117. }
  118. return pis.ToArray();
  119. }
  120. public static MethodInfo PropertyGetMethod(this PropertyInfo pi, bool nonPublic = false)
  121. {
  122. return pi.GetMethod;
  123. }
  124. public static object[] AllAttributes(this PropertyInfo propertyInfo)
  125. {
  126. return propertyInfo.GetCustomAttributes(true).ToArray();
  127. }
  128. public static object[] AllAttributes(this Type type)
  129. {
  130. return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
  131. }
  132. public static List<TAttr> AllAttributes<TAttr>(this Type type)
  133. where TAttr : Attribute
  134. {
  135. return type.GetTypeInfo().GetCustomAttributes<TAttr>(true).ToList();
  136. }
  137. }
  138. }