2
0

ReflectionExtensions.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using Jint.Native;
  7. using Jint.Runtime;
  8. namespace Jint.Extensions
  9. {
  10. internal static class ReflectionExtensions
  11. {
  12. private static readonly Type nullableType = typeof(Nullable<>);
  13. internal static void SetValue(this MemberInfo memberInfo, object forObject, object value)
  14. {
  15. if (memberInfo.MemberType == MemberTypes.Field)
  16. {
  17. var fieldInfo = (FieldInfo) memberInfo;
  18. if (value != null && fieldInfo.FieldType.IsInstanceOfType(value))
  19. {
  20. fieldInfo.SetValue(forObject, value);
  21. }
  22. }
  23. else if (memberInfo.MemberType == MemberTypes.Property)
  24. {
  25. var propertyInfo = (PropertyInfo) memberInfo;
  26. if (value != null && propertyInfo.PropertyType.IsInstanceOfType(value))
  27. {
  28. propertyInfo.SetValue(forObject, value);
  29. }
  30. }
  31. }
  32. internal static Type GetDefinedType(this MemberInfo memberInfo)
  33. {
  34. return memberInfo switch
  35. {
  36. PropertyInfo propertyInfo => propertyInfo.PropertyType,
  37. FieldInfo fieldInfo => fieldInfo.FieldType,
  38. _ => null
  39. };
  40. }
  41. internal static IEnumerable<MethodInfo> GetExtensionMethods(this Type type)
  42. {
  43. return type.GetMethods(BindingFlags.Public | BindingFlags.Static)
  44. .Where(m => m.IsExtensionMethod());
  45. }
  46. internal static IEnumerable<MethodInfo> GetOperatorOverloadMethods(this Type type)
  47. {
  48. return type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
  49. .Where(m => m.IsSpecialName);
  50. }
  51. private static bool IsExtensionMethod(this MethodBase methodInfo)
  52. {
  53. return methodInfo.IsDefined(typeof(ExtensionAttribute), true);
  54. }
  55. public static bool IsNullable(this Type type)
  56. {
  57. return type is { IsGenericType: true } && type.GetGenericTypeDefinition() == nullableType;
  58. }
  59. public static bool IsNumeric(this Type type)
  60. {
  61. if (type == null || type.IsEnum)
  62. {
  63. return false;
  64. }
  65. switch (Type.GetTypeCode(type))
  66. {
  67. case TypeCode.Byte:
  68. case TypeCode.Decimal:
  69. case TypeCode.Double:
  70. case TypeCode.Int16:
  71. case TypeCode.Int32:
  72. case TypeCode.Int64:
  73. case TypeCode.SByte:
  74. case TypeCode.Single:
  75. case TypeCode.UInt16:
  76. case TypeCode.UInt32:
  77. case TypeCode.UInt64:
  78. return true;
  79. default:
  80. return false;
  81. }
  82. }
  83. public static bool IsClrNumericCoercible(this Type type)
  84. {
  85. if (type == null || type.IsEnum)
  86. {
  87. return false;
  88. }
  89. switch (Type.GetTypeCode(type))
  90. {
  91. case TypeCode.Decimal:
  92. case TypeCode.Double:
  93. case TypeCode.Int32:
  94. case TypeCode.Int64:
  95. return true;
  96. default:
  97. return false;
  98. }
  99. }
  100. public static object AsNumberOfType(this double d, TypeCode type)
  101. {
  102. switch (type)
  103. {
  104. case TypeCode.Decimal:
  105. return (decimal) d;
  106. case TypeCode.Double:
  107. return d;
  108. case TypeCode.Int32:
  109. return (int) d;
  110. case TypeCode.Int64:
  111. return (long) d;
  112. default:
  113. ExceptionHelper.ThrowArgumentException("Cannot convert " + type);
  114. return null;
  115. }
  116. }
  117. public static bool TryConvertViaTypeCoercion(
  118. Type _memberType,
  119. ValueCoercionType valueCoercionType,
  120. JsValue value,
  121. out object converted)
  122. {
  123. if (_memberType == typeof(bool) && (valueCoercionType & ValueCoercionType.Boolean) != 0)
  124. {
  125. converted = TypeConverter.ToBoolean(value);
  126. return true;
  127. }
  128. if (_memberType == typeof(string)
  129. && !value.IsNullOrUndefined()
  130. && (valueCoercionType & ValueCoercionType.String) != 0)
  131. {
  132. // we know how to print out correct string presentation for primitives
  133. // that are non-null and non-undefined
  134. converted = TypeConverter.ToString(value);
  135. return true;
  136. }
  137. if (_memberType.IsClrNumericCoercible() && (valueCoercionType & ValueCoercionType.Number) != 0)
  138. {
  139. // we know how to print out correct string presentation for primitives
  140. // that are non-null and non-undefined
  141. var number = TypeConverter.ToNumber(value);
  142. converted = number.AsNumberOfType(Type.GetTypeCode(_memberType));
  143. return true;
  144. }
  145. converted = null;
  146. return false;
  147. }
  148. }
  149. }