TypeHelpers.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Reflection;
  3. namespace UnityEngine.InputSystem.Utilities
  4. {
  5. internal static class TypeHelpers
  6. {
  7. public static TObject As<TObject>(this System.Object obj)
  8. {
  9. return (TObject)obj;
  10. }
  11. public static bool IsInt(this TypeCode type)
  12. {
  13. switch (type)
  14. {
  15. case TypeCode.Byte: return true;
  16. case TypeCode.SByte: return true;
  17. case TypeCode.Int16: return true;
  18. case TypeCode.Int32: return true;
  19. case TypeCode.Int64: return true;
  20. case TypeCode.UInt16: return true;
  21. case TypeCode.UInt32: return true;
  22. case TypeCode.UInt64: return true;
  23. }
  24. return false;
  25. }
  26. public static Type GetValueType(MemberInfo member)
  27. {
  28. var field = member as FieldInfo;
  29. if (field != null)
  30. return field.FieldType;
  31. var property = member as PropertyInfo;
  32. if (property != null)
  33. return property.PropertyType;
  34. var method = member as MethodInfo;
  35. if (method != null)
  36. return method.ReturnType;
  37. return null;
  38. }
  39. public static string GetNiceTypeName(Type type)
  40. {
  41. if (type.IsPrimitive)
  42. {
  43. if (type == typeof(int))
  44. return "int";
  45. if (type == typeof(float))
  46. return "float";
  47. if (type == typeof(char))
  48. return "char";
  49. if (type == typeof(byte))
  50. return "byte";
  51. if (type == typeof(short))
  52. return "short";
  53. if (type == typeof(long))
  54. return "long";
  55. if (type == typeof(double))
  56. return "double";
  57. if (type == typeof(uint))
  58. return "uint";
  59. if (type == typeof(sbyte))
  60. return "sbyte";
  61. if (type == typeof(ushort))
  62. return "ushort";
  63. if (type == typeof(ulong))
  64. return "ulong";
  65. }
  66. return type.Name;
  67. }
  68. public static Type GetGenericTypeArgumentFromHierarchy(Type type, Type genericTypeDefinition, int argumentIndex)
  69. {
  70. if (type == null)
  71. throw new ArgumentNullException(nameof(type));
  72. if (genericTypeDefinition == null)
  73. throw new ArgumentNullException(nameof(genericTypeDefinition));
  74. if (argumentIndex < 0)
  75. throw new ArgumentOutOfRangeException(nameof(argumentIndex));
  76. if (genericTypeDefinition.IsInterface)
  77. {
  78. // Walk up the chain until we find the generic type def as an interface on a type.
  79. while (true)
  80. {
  81. var interfaces = type.GetInterfaces();
  82. var haveFoundInterface = false;
  83. foreach (var element in interfaces)
  84. {
  85. if (element.IsConstructedGenericType &&
  86. element.GetGenericTypeDefinition() == genericTypeDefinition)
  87. {
  88. type = element;
  89. haveFoundInterface = true;
  90. break;
  91. }
  92. // Recurse into interface in case we're looking for a base interface.
  93. var typeArgument =
  94. GetGenericTypeArgumentFromHierarchy(element, genericTypeDefinition, argumentIndex);
  95. if (typeArgument != null)
  96. return typeArgument;
  97. }
  98. if (haveFoundInterface)
  99. break;
  100. type = type.BaseType;
  101. if (type == null || type == typeof(object))
  102. return null;
  103. }
  104. }
  105. else
  106. {
  107. // Walk up the chain until we find the generic type def.
  108. while (!type.IsConstructedGenericType || type.GetGenericTypeDefinition() != genericTypeDefinition)
  109. {
  110. type = type.BaseType;
  111. if (type == typeof(object))
  112. return null;
  113. }
  114. }
  115. return type.GenericTypeArguments[argumentIndex];
  116. }
  117. }
  118. }