Created
February 2, 2012 22:24
-
-
Save takeshik/1726173 to your computer and use it in GitHub Desktop.
Numeric Conversions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static Type ConvertNumericType(Type expressionType, Type expectedType) | |
{ | |
var expectedType_ = Type.GetTypeCode(Nullable.GetUnderlyingType(expectedType) ?? expectedType); | |
switch (Type.GetTypeCode(Nullable.GetUnderlyingType(expressionType) ?? expressionType)) | |
{ | |
case TypeCode.Char: | |
switch (expectedType_) | |
{ | |
case TypeCode.Char: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
case TypeCode.UInt64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.SByte: | |
switch (expectedType_) | |
{ | |
case TypeCode.SByte: | |
case TypeCode.Int16: | |
case TypeCode.Int32: | |
case TypeCode.Int64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.Byte: | |
switch (expectedType_) | |
{ | |
case TypeCode.Byte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
case TypeCode.UInt64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.Int16: | |
switch (expectedType_) | |
{ | |
case TypeCode.Int16: | |
case TypeCode.Int32: | |
case TypeCode.Int64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.UInt16: | |
switch (expectedType_) | |
{ | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
case TypeCode.UInt64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.Int32: | |
switch (expectedType_) | |
{ | |
case TypeCode.Int32: | |
case TypeCode.Int64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.UInt32: | |
switch (expectedType_) | |
{ | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
case TypeCode.UInt64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.Int64: | |
switch (expectedType_) | |
{ | |
case TypeCode.Int64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.UInt64: | |
switch (expectedType_) | |
{ | |
case TypeCode.UInt64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.Single: | |
switch (expectedType_) | |
{ | |
case TypeCode.Single: | |
case TypeCode.Double: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
case TypeCode.Double: | |
switch (expectedType_) | |
{ | |
case TypeCode.Char: | |
case TypeCode.SByte: | |
case TypeCode.Byte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
case TypeCode.UInt64: | |
case TypeCode.Single: | |
case TypeCode.Double: | |
case TypeCode.Decimal: | |
return expectedType; | |
default: | |
return expectedType; | |
} | |
default: | |
return null; | |
} | |
} | |
internal static Type ConvertNumericTypeForAlithmetics(Type type, Boolean needsSigned) | |
{ | |
var isNullable = Nullable.GetUnderlyingType(type) != null; | |
switch (Type.GetTypeCode(isNullable ? Nullable.GetUnderlyingType(type) : type)) | |
{ | |
case TypeCode.Char: | |
case TypeCode.SByte: | |
case TypeCode.Byte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
return isNullable | |
? typeof(Nullable<Int32>) | |
: typeof(Int32); | |
case TypeCode.UInt32: | |
return needsSigned | |
? isNullable | |
? typeof(Nullable<Int64>) | |
: typeof(Int64) | |
: isNullable | |
? typeof(Nullable<UInt32>) | |
: typeof(UInt32); | |
case TypeCode.Int64: | |
return isNullable | |
? typeof(Nullable<Int64>) | |
: typeof(Int64); | |
case TypeCode.UInt64: | |
return needsSigned | |
? null | |
: isNullable | |
? typeof(Nullable<UInt64>) | |
: typeof(UInt64); | |
default: | |
return null; | |
} | |
} | |
internal static Type ConvertNumericTypeForAlithmetics(Type leftType, Type rightType) | |
{ | |
var isNullable = Nullable.GetUnderlyingType(leftType) != null || Nullable.GetUnderlyingType(rightType) != null; | |
var rightType_ = Type.GetTypeCode(Nullable.GetUnderlyingType(rightType) ?? rightType); | |
switch (Type.GetTypeCode(Nullable.GetUnderlyingType(leftType) ?? leftType)) | |
{ | |
case TypeCode.Char: | |
case TypeCode.SByte: | |
case TypeCode.Byte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
switch (rightType_) | |
{ | |
case TypeCode.Char: | |
case TypeCode.SByte: | |
case TypeCode.Byte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
return isNullable | |
? typeof(Nullable<Int32>) | |
: typeof(Int32); | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
return isNullable | |
? typeof(Nullable<Int64>) | |
: typeof(Int64); | |
default: | |
return null; | |
} | |
case TypeCode.UInt32: | |
switch (rightType_) | |
{ | |
case TypeCode.Char: | |
case TypeCode.Byte: | |
case TypeCode.UInt16: | |
case TypeCode.UInt32: | |
return isNullable | |
? typeof(Nullable<UInt32>) | |
: typeof(UInt32); | |
case TypeCode.SByte: | |
case TypeCode.Int16: | |
case TypeCode.Int32: | |
case TypeCode.Int64: | |
return isNullable | |
? typeof(Nullable<Int64>) | |
: typeof(Int64); | |
case TypeCode.UInt64: | |
return isNullable | |
? typeof(Nullable<UInt64>) | |
: typeof(UInt64); | |
default: | |
return null; | |
} | |
case TypeCode.Int64: | |
switch (rightType_) | |
{ | |
case TypeCode.Char: | |
case TypeCode.SByte: | |
case TypeCode.Byte: | |
case TypeCode.Int16: | |
case TypeCode.UInt16: | |
case TypeCode.Int32: | |
case TypeCode.UInt32: | |
case TypeCode.Int64: | |
return isNullable | |
? typeof(Nullable<Int64>) | |
: typeof(Int64); | |
default: | |
return null; | |
} | |
case TypeCode.UInt64: | |
switch (rightType_) | |
{ | |
case TypeCode.Char: | |
case TypeCode.Byte: | |
case TypeCode.UInt16: | |
case TypeCode.UInt32: | |
case TypeCode.UInt64: | |
return isNullable | |
? typeof(Nullable<UInt64>) | |
: typeof(UInt64); | |
default: | |
return null; | |
} | |
default: | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment