1 using System
.Reflection
;
5 partial class DefaultBinder
7 // InvokeUtil::PrimitiveAttributes from coreclr
8 static readonly int[] PrimitiveAttributes
= {
9 0x00, // ELEMENT_TYPE_END
10 0x00, // ELEMENT_TYPE_VOID
11 0x0004, // ELEMENT_TYPE_BOOLEAN
12 0x3F88, // ELEMENT_TYPE_CHAR (W = U2, CHAR, I4, U4, I8, U8, R4, R8) (U2 == Char)
13 0x3550, // ELEMENT_TYPE_I1 (W = I1, I2, I4, I8, R4, R8)
14 0x3FE8, // ELEMENT_TYPE_U1 (W = CHAR, U1, I2, U2, I4, U4, I8, U8, R4, R8)
15 0x3540, // ELEMENT_TYPE_I2 (W = I2, I4, I8, R4, R8)
16 0x3F88, // ELEMENT_TYPE_U2 (W = U2, CHAR, I4, U4, I8, U8, R4, R8)
17 0x3500, // ELEMENT_TYPE_I4 (W = I4, I8, R4, R8)
18 0x3E00, // ELEMENT_TYPE_U4 (W = U4, I8, R4, R8)
19 0x3400, // ELEMENT_TYPE_I8 (W = I8, R4, R8)
20 0x3800, // ELEMENT_TYPE_U8 (W = U8, R4, R8)
21 0x3000, // ELEMENT_TYPE_R4 (W = R4, R8)
22 0x2000, // ELEMENT_TYPE_R8 (W = R8)
25 static bool IsPrimitiveType (CorElementType type
) {
26 return (type
>= CorElementType
.Void
&& type
<= CorElementType
.R8
) || type
== CorElementType
.I
|| type
== CorElementType
.U
;
29 static bool CanChangePrimitive (Type source
, Type target
)
31 var src
= RuntimeTypeHandle
.GetCorElementType ((RuntimeType
)source
);
32 var dst
= RuntimeTypeHandle
.GetCorElementType ((RuntimeType
)target
);
34 if (!IsPrimitiveType (dst
) || !IsPrimitiveType (src
))
39 return ((1 << (int)dst
) | PrimitiveAttributes
[(int)src
]) != 0;
42 private static bool CanChangePrimitiveObjectToType(object source
, Type type
)
48 return CanChangePrimitive (Enum
.GetUnderlyingType (source
.GetType ()), type
);
50 return CanChangePrimitive (source
.GetType (), type
);