TypeExtensions.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. namespace XLua
  5. {
  6. internal static class TypeExtensions
  7. {
  8. public static bool IsValueType(this Type type)
  9. {
  10. #if !UNITY_WSA || UNITY_EDITOR
  11. return type.IsValueType;
  12. #else
  13. return type.GetTypeInfo().IsValueType;
  14. #endif
  15. }
  16. public static bool IsEnum(this Type type)
  17. {
  18. #if !UNITY_WSA || UNITY_EDITOR
  19. return type.IsEnum;
  20. #else
  21. return type.GetTypeInfo().IsEnum;
  22. #endif
  23. }
  24. public static bool IsPrimitive(this Type type)
  25. {
  26. #if !UNITY_WSA || UNITY_EDITOR
  27. return type.IsPrimitive;
  28. #else
  29. return type.GetTypeInfo().IsPrimitive;
  30. #endif
  31. }
  32. public static bool IsAbstract(this Type type)
  33. {
  34. #if !UNITY_WSA || UNITY_EDITOR
  35. return type.IsAbstract;
  36. #else
  37. return type.GetTypeInfo().IsAbstract;
  38. #endif
  39. }
  40. public static bool IsSealed(this Type type)
  41. {
  42. #if !UNITY_WSA || UNITY_EDITOR
  43. return type.IsSealed;
  44. #else
  45. return type.GetTypeInfo().IsSealed;
  46. #endif
  47. }
  48. public static bool IsInterface(this Type type)
  49. {
  50. #if !UNITY_WSA || UNITY_EDITOR
  51. return type.IsInterface;
  52. #else
  53. return type.GetTypeInfo().IsInterface;
  54. #endif
  55. }
  56. public static bool IsClass(this Type type)
  57. {
  58. #if !UNITY_WSA || UNITY_EDITOR
  59. return type.IsClass;
  60. #else
  61. return type.GetTypeInfo().IsClass;
  62. #endif
  63. }
  64. public static Type BaseType(this Type type)
  65. {
  66. #if !UNITY_WSA || UNITY_EDITOR
  67. return type.BaseType;
  68. #else
  69. return type.GetTypeInfo().BaseType;
  70. #endif
  71. }
  72. public static bool IsGenericType(this Type type)
  73. {
  74. #if !UNITY_WSA || UNITY_EDITOR
  75. return type.IsGenericType;
  76. #else
  77. return type.GetTypeInfo().IsGenericType;
  78. #endif
  79. }
  80. public static bool IsGenericTypeDefinition(this Type type)
  81. {
  82. #if !UNITY_WSA || UNITY_EDITOR
  83. return type.IsGenericTypeDefinition;
  84. #else
  85. return type.GetTypeInfo().IsGenericTypeDefinition;
  86. #endif
  87. }
  88. #if UNITY_WSA && !UNITY_EDITOR
  89. public static bool IsSubclassOf(this Type type, Type c)
  90. {
  91. return type.GetTypeInfo().IsSubclassOf(c);
  92. }
  93. public static bool IsDefined(this Type type, Type attributeType, bool inherit)
  94. {
  95. return type.GetTypeInfo().IsDefined(attributeType, inherit);
  96. }
  97. public static Type[] GetGenericParameterConstraints(this Type type)
  98. {
  99. return type.GetTypeInfo().GetGenericParameterConstraints();
  100. }
  101. #endif
  102. public static bool IsNestedPublic(this Type type)
  103. {
  104. #if !UNITY_WSA || UNITY_EDITOR
  105. return type.IsNestedPublic;
  106. #else
  107. return type.GetTypeInfo().IsNestedPublic;
  108. #endif
  109. }
  110. public static bool IsPublic(this Type type)
  111. {
  112. #if !UNITY_WSA || UNITY_EDITOR
  113. return type.IsPublic;
  114. #else
  115. return type.GetTypeInfo().IsPublic;
  116. #endif
  117. }
  118. public static string GetFriendlyName(this Type type)
  119. {
  120. if (type == typeof(int))
  121. return "int";
  122. else if (type == typeof(short))
  123. return "short";
  124. else if (type == typeof(byte))
  125. return "byte";
  126. else if (type == typeof(bool))
  127. return "bool";
  128. else if (type == typeof(long))
  129. return "long";
  130. else if (type == typeof(float))
  131. return "float";
  132. else if (type == typeof(double))
  133. return "double";
  134. else if (type == typeof(decimal))
  135. return "decimal";
  136. else if (type == typeof(string))
  137. return "string";
  138. else if (type.IsGenericType())
  139. return type.FullName.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments()
  140. .Select(x => GetFriendlyName(x)).ToArray()) + ">";
  141. else
  142. return type.FullName;
  143. }
  144. }
  145. }