ReflectionExtensions.cs 5.3 KB

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