1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Reflection;
namespace System
{
public sealed partial class DefaultBinder : Binder
{
// CanChangePrimitive
// This will determine if the source can be converted to the target type
private static bool CanChangePrimitive(Type source, Type target)
{
return CanPrimitiveWiden(source, target);
}
// CanChangePrimitiveObjectToType
private static bool CanChangePrimitiveObjectToType(object source, Type type)
{
return CanPrimitiveWiden(source.GetType(), type);
}
private static bool CanPrimitiveWiden(Type source, Type target)
{
Primitives widerCodes = _primitiveConversions[(int)(Type.GetTypeCode(source))];
Primitives targetCode = (Primitives)(1 << (int)(Type.GetTypeCode(target)));
return 0 != (widerCodes & targetCode);
}
[Flags]
private enum Primitives
{
Boolean = 1 << (int)TypeCode.Boolean,
Char = 1 << (int)TypeCode.Char,
SByte = 1 << (int)TypeCode.SByte,
Byte = 1 << (int)TypeCode.Byte,
Int16 = 1 << (int)TypeCode.Int16,
UInt16 = 1 << (int)TypeCode.UInt16,
Int32 = 1 << (int)TypeCode.Int32,
UInt32 = 1 << (int)TypeCode.UInt32,
Int64 = 1 << (int)TypeCode.Int64,
UInt64 = 1 << (int)TypeCode.UInt64,
Single = 1 << (int)TypeCode.Single,
Double = 1 << (int)TypeCode.Double,
Decimal = 1 << (int)TypeCode.Decimal,
DateTime = 1 << (int)TypeCode.DateTime,
String = 1 << (int)TypeCode.String,
}
private static Primitives[] _primitiveConversions = new Primitives[]
{
/* Empty */ 0, // not primitive
/* Object */ 0, // not primitive
/* DBNull */ 0, // not exposed.
/* Boolean */ Primitives.Boolean,
/* Char */ Primitives.Char | Primitives.UInt16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* SByte */ Primitives.SByte | Primitives.Int16 | Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Byte */ Primitives.Byte | Primitives.Char | Primitives.UInt16 | Primitives.Int16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Int16 */ Primitives.Int16 | Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* UInt16 */ Primitives.UInt16 | Primitives.UInt32 | Primitives.Int32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Int32 */ Primitives.Int32 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* UInt32 */ Primitives.UInt32 | Primitives.UInt64 | Primitives.Int64 | Primitives.Single | Primitives.Double,
/* Int64 */ Primitives.Int64 | Primitives.Single | Primitives.Double,
/* UInt64 */ Primitives.UInt64 | Primitives.Single | Primitives.Double,
/* Single */ Primitives.Single | Primitives.Double,
/* Double */ Primitives.Double,
/* Decimal */ Primitives.Decimal,
/* DateTime */ Primitives.DateTime,
/* [Unused] */ 0,
/* String */ Primitives.String,
};
}
}
|