Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NetFXCoreWrappers.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Runtime.CompilerServices;
  5. using Svelto.DataStructures;
  6. public static class NetFXCoreWrappers
  7. {
  8. public static Type GetDeclaringType(this MethodInfo methodInfo)
  9. {
  10. #if NETFX_CORE
  11. return methodInfo.DeclaringType;
  12. #else
  13. return methodInfo.ReflectedType;
  14. #endif
  15. }
  16. public static MethodInfo GetMethodInfoEx(this Delegate delegateEx)
  17. {
  18. #if NETFX_CORE
  19. var method = delegateEx.GetMethodInfo();
  20. #else
  21. var method = delegateEx.Method;
  22. #endif
  23. return method;
  24. }
  25. public static Type[] GetInterfacesEx(this Type type)
  26. {
  27. #if NETFX_CORE
  28. return type.GetInterfaces();
  29. #else
  30. return type.GetInterfaces();
  31. #endif
  32. }
  33. public static bool IsInterfaceEx(this Type type)
  34. {
  35. #if NETFX_CORE
  36. return type.GetTypeInfo().IsInterface;
  37. #else
  38. return type.IsInterface;
  39. #endif
  40. }
  41. public static bool IsValueTypeEx(this Type type)
  42. {
  43. #if NETFX_CORE
  44. return type.GetTypeInfo().IsValueType;
  45. #else
  46. return type.IsValueType;
  47. #endif
  48. }
  49. public static Type GetDeclaringType(this MemberInfo memberInfo)
  50. {
  51. #if NETFX_CORE
  52. return memberInfo.DeclaringType;
  53. #else
  54. return memberInfo.ReflectedType;
  55. #endif
  56. }
  57. public static Type GetBaseType(this Type type)
  58. {
  59. #if NETFX_CORE
  60. return type.GetTypeInfo().BaseType;
  61. #else
  62. return type.BaseType;
  63. #endif
  64. }
  65. public static IEnumerable<Attribute> GetCustomAttributes(this Type type, bool inherit)
  66. {
  67. #if !NETFX_CORE
  68. return Attribute.GetCustomAttributes(type, inherit);
  69. #else
  70. return type.GetTypeInfo().GetCustomAttributes(inherit);
  71. #endif
  72. }
  73. public static bool ContainsCustomAttribute(this MemberInfo memberInfo, Type customAttribute, bool inherit)
  74. {
  75. #if !NETFX_CORE
  76. return Attribute.IsDefined(memberInfo, customAttribute, inherit);
  77. #else
  78. return memberInfo.GetCustomAttribute(customAttribute, inherit) != null;
  79. #endif
  80. }
  81. public static bool IsGenericTypeEx(this Type type)
  82. {
  83. #if !NETFX_CORE
  84. return type.IsGenericType;
  85. #else
  86. return type.IsConstructedGenericType;
  87. #endif
  88. }
  89. public static Type[] GetGenericArgumentsEx(this Type type)
  90. {
  91. #if !NETFX_CORE
  92. return type.GetGenericArguments();
  93. #else
  94. var typeinfo = type.GetTypeInfo();
  95. return typeinfo.IsGenericTypeDefinition
  96. ? typeinfo.GenericTypeParameters
  97. : typeinfo.GenericTypeArguments;
  98. #endif
  99. }
  100. public static MemberInfo[] FindWritablePropertiesWithCustomAttribute(this Type contract,
  101. Type customAttributeType)
  102. {
  103. FasterList<MemberInfo> propertyList = new FasterList<MemberInfo>(8);
  104. do
  105. {
  106. var propertyInfos = contract.GetProperties(System.Reflection.BindingFlags.Public |
  107. System.Reflection.BindingFlags.NonPublic |
  108. System.Reflection.BindingFlags.DeclaredOnly |
  109. System.Reflection.BindingFlags.Instance);
  110. for (int i = 0; i < propertyInfos.Length; i++)
  111. {
  112. PropertyInfo propertyInfo = propertyInfos[i];
  113. if (propertyInfo.CanWrite &&
  114. propertyInfo.ContainsCustomAttribute(customAttributeType, false) == true)
  115. propertyList.Add(propertyInfo);
  116. }
  117. contract = contract.GetBaseType();
  118. } while (contract != null);
  119. if (propertyList.Count > 0)
  120. return propertyList.ToArray();
  121. return null;
  122. }
  123. public static bool IsCompilerGenerated(this Type t)
  124. {
  125. #if NETFX_CORE
  126. var attr = t.GetTypeInfo().GetCustomAttribute(typeof(CompilerGeneratedAttribute));
  127. return attr != null;
  128. #else
  129. var attr = Attribute.IsDefined(t, typeof(CompilerGeneratedAttribute));
  130. return attr;
  131. #endif
  132. }
  133. public static bool IsCompilerGenerated(this MemberInfo memberInfo)
  134. {
  135. #if NETFX_CORE
  136. var attr = memberInfo.DeclaringType.GetTypeInfo().GetCustomAttribute(_compilerType);
  137. return attr != null;
  138. #else
  139. var attr = Attribute.IsDefined(memberInfo, _compilerType);
  140. return attr;
  141. #endif
  142. }
  143. static readonly Type _compilerType = typeof(CompilerGeneratedAttribute);
  144. }