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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading;
  6. using Svelto.DataStructures;
  7. namespace Svelto.Utilities
  8. {
  9. public static class NetFXCoreWrappers
  10. {
  11. public static MethodInfo GetMethodInfoEx(this Delegate delegateEx)
  12. {
  13. #if NETFX_CORE
  14. var method = delegateEx.GetMethodInfo();
  15. #else
  16. var method = delegateEx.Method;
  17. #endif
  18. return method;
  19. }
  20. public static Type GetDeclaringType(this MemberInfo memberInfo)
  21. {
  22. #if NETFX_CORE
  23. return memberInfo.DeclaringType;
  24. #else
  25. return memberInfo.ReflectedType;
  26. #endif
  27. }
  28. public static Type GetBaseType(this Type type)
  29. {
  30. #if NETFX_CORE
  31. return type.BaseType();
  32. #else
  33. return type.BaseType;
  34. #endif
  35. }
  36. public static IEnumerable<Attribute> GetCustomAttributes(this Type type, bool inherit)
  37. {
  38. #if !NETFX_CORE
  39. return Attribute.GetCustomAttributes(type, inherit);
  40. #else
  41. return type.GetTypeInfo().GetCustomAttributes(inherit);
  42. #endif
  43. }
  44. public static bool ContainsCustomAttribute(this MemberInfo memberInfo, Type customAttribute, bool inherit)
  45. {
  46. #if !NETFX_CORE
  47. return Attribute.IsDefined(memberInfo, customAttribute, inherit);
  48. #else
  49. return memberInfo.GetCustomAttribute(customAttribute, inherit) != null;
  50. #endif
  51. }
  52. public static bool IsGenericTypeEx(this Type type)
  53. {
  54. #if !NETFX_CORE
  55. return type.IsGenericType;
  56. #else
  57. return type.IsConstructedGenericType;
  58. #endif
  59. }
  60. public static MemberInfo[] FindWritablePropertiesWithCustomAttribute(this Type contract,
  61. Type customAttributeType)
  62. {
  63. FasterList<MemberInfo> propertyList = new FasterList<MemberInfo>(8);
  64. do
  65. {
  66. var propertyInfos = contract.GetProperties(System.Reflection.BindingFlags.Public |
  67. System.Reflection.BindingFlags.NonPublic |
  68. System.Reflection.BindingFlags.DeclaredOnly |
  69. System.Reflection.BindingFlags.Instance);
  70. for (int i = 0; i < propertyInfos.Length; i++)
  71. {
  72. PropertyInfo propertyInfo = propertyInfos[i];
  73. if (propertyInfo.CanWrite &&
  74. propertyInfo.ContainsCustomAttribute(customAttributeType, false) == true)
  75. propertyList.Add(propertyInfo);
  76. }
  77. contract = contract.GetBaseType();
  78. } while (contract != null);
  79. if (propertyList.Count > 0)
  80. return propertyList.ToArray();
  81. return null;
  82. }
  83. public static bool IsCompilerGenerated(this Type t)
  84. {
  85. #if NETFX_CORE
  86. var attr = t.GetTypeInfo().GetCustomAttribute(typeof(CompilerGeneratedAttribute));
  87. return attr != null;
  88. #else
  89. var attr = Attribute.IsDefined(t, typeof(CompilerGeneratedAttribute));
  90. return attr;
  91. #endif
  92. }
  93. public static bool IsCompilerGenerated(this MemberInfo memberInfo)
  94. {
  95. #if NETFX_CORE
  96. var attr = memberInfo.DeclaringType.GetTypeInfo().GetCustomAttribute(_compilerType);
  97. return attr != null;
  98. #else
  99. var attr = Attribute.IsDefined(memberInfo, _compilerType);
  100. return attr;
  101. #endif
  102. }
  103. static readonly Type _compilerType = typeof(CompilerGeneratedAttribute);
  104. }
  105. }