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

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