本文整理汇总了C#中IConvertible.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# IConvertible.ToString方法的具体用法?C# IConvertible.ToString怎么用?C# IConvertible.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConvertible
的用法示例。
在下文中一共展示了IConvertible.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddNumber
public override string AddNumber(IConvertible number, IList<DbParameter> parameters)
{
if (number == null)
{
throw new ArgumentNullException("number");
}
return number.ToString();
}
示例2: DoBinOp
protected override IConvertible DoBinOp(IConvertible left, IConvertible right)
{
TypeCode typeCode = GetValueType(left, right);
switch (typeCode)
{
case TypeCode.SByte:
return (Convert.ToSByte(left) + Convert.ToSByte(right));
case TypeCode.Byte:
return (Convert.ToByte(left) + Convert.ToByte(right));
case TypeCode.Int16:
return (Convert.ToInt16(left) + Convert.ToInt16(right));
case TypeCode.UInt16:
return (Convert.ToUInt16(left) + Convert.ToUInt16(right));
case TypeCode.Int32:
return (Convert.ToInt32(left) + Convert.ToInt32(right));
case TypeCode.UInt32:
return (Convert.ToUInt32(left) + Convert.ToUInt32(right));
case TypeCode.Int64:
return (Convert.ToInt64(left) + Convert.ToInt64(right));
case TypeCode.UInt64:
return (Convert.ToUInt64(left) + Convert.ToUInt64(right));
case TypeCode.Double:
return (Convert.ToDouble(left) + Convert.ToDouble(right));
case TypeCode.Decimal:
return (Convert.ToDecimal(left) + Convert.ToDecimal(right));
}
return left.ToString() + right.ToString();
}
示例3: JScriptStrictEquals
internal static bool JScriptStrictEquals(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects){
switch (t1){
case TypeCode.Empty: return t2 == TypeCode.Empty;
case TypeCode.Object:
if (v1 == v2) return true;
if (v1 is Missing || v1 is System.Reflection.Missing) v1 = null;
if (v1 == v2) return true;
if (v2 is Missing || v2 is System.Reflection.Missing) v2 = null;
return v1 == v2;
case TypeCode.DBNull: return t2 == TypeCode.DBNull;
case TypeCode.Boolean: return t2 == TypeCode.Boolean && ic1.ToBoolean(null) == ic2.ToBoolean(null);
case TypeCode.Char:
Char ch = ic1.ToChar(null);
switch(t2){
case TypeCode.Char: return ch == ic2.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64: return ch == ic2.ToInt64(null);
case TypeCode.UInt64: return ch == ic2.ToUInt64(null);
case TypeCode.Single:
case TypeCode.Double: return ch == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)(int)ch) == ic2.ToDecimal(null);
case TypeCode.String:
String str = ic2.ToString(null);
return str.Length == 1 && ch == str[0];
}
return false;
case TypeCode.SByte:
SByte sb1 = ic1.ToSByte(null);
switch (t2){
case TypeCode.Char: return sb1 == ic2.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64: return sb1 == ic2.ToInt64(null);
case TypeCode.UInt64: return sb1 >= 0 && ((UInt64)sb1) == ic2.ToUInt64(null);
case TypeCode.Single: return sb1 == ic2.ToSingle(null);
case TypeCode.Double: return sb1 == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)sb1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.Byte:
Byte b1 = ic1.ToByte(null);
switch (t2){
case TypeCode.Char: return b1 == ic2.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64: return b1 == ic2.ToInt64(null);
case TypeCode.UInt64: return b1 == ic2.ToUInt64(null);
case TypeCode.Single: return b1 == ic2.ToSingle(null);
case TypeCode.Double: return b1 == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)b1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.Int16:
Int16 s1 = ic1.ToInt16(null);
switch (t2){
case TypeCode.Char: return s1 == ic2.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64: return s1 == ic2.ToInt64(null);
case TypeCode.UInt64: return s1 >= 0 && ((UInt64)s1) == ic2.ToUInt64(null);
case TypeCode.Single: return s1 == ic2.ToSingle(null);
case TypeCode.Double: return s1 == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)s1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.UInt16:
UInt16 us1 = ic1.ToUInt16(null);
switch (t2){
case TypeCode.Char: return us1 == ic2.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64: return us1 == ic2.ToInt64(null);
case TypeCode.UInt64: return us1 == ic2.ToUInt64(null);
case TypeCode.Single: return us1 == ic2.ToSingle(null);
//.........这里部分代码省略.........
示例4: SetAsIConvertible
public void SetAsIConvertible(IConvertible value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
TypeCode tc = value.GetTypeCode();
CultureInfo ci = CultureInfo.CurrentCulture;
switch (tc) {
case TypeCode.Empty: break;
case TypeCode.Object: AsUnknown = value; break;
case TypeCode.DBNull: SetAsNull(); break;
case TypeCode.Boolean: AsBool = value.ToBoolean(ci); break;
case TypeCode.Char: AsUi2 = value.ToChar(ci); break;
case TypeCode.SByte: AsI1 = value.ToSByte(ci); break;
case TypeCode.Byte: AsUi1 = value.ToByte(ci); break;
case TypeCode.Int16: AsI2 = value.ToInt16(ci); break;
case TypeCode.UInt16: AsUi2 = value.ToUInt16(ci); break;
case TypeCode.Int32: AsI4 = value.ToInt32(ci); break;
case TypeCode.UInt32: AsUi4 = value.ToUInt32(ci); break;
case TypeCode.Int64: AsI8 = value.ToInt64(ci); break;
case TypeCode.UInt64: AsI8 = value.ToInt64(ci); break;
case TypeCode.Single: AsR4 = value.ToSingle(ci); break;
case TypeCode.Double: AsR8 = value.ToDouble(ci); break;
case TypeCode.Decimal: AsDecimal = value.ToDecimal(ci); break;
case TypeCode.DateTime: AsDate = value.ToDateTime(ci); break;
case TypeCode.String: AsBstr = value.ToString(ci); break;
default:
throw Assert.Unreachable;
}
}
示例5: ToString
internal static String ToString(Object value, PreferredType pref, IConvertible ic, bool explicitOK){
Enum e = value as Enum;
if (e != null) return e.ToString("G");
EnumWrapper ew = value as EnumWrapper;
if (ew != null) return ew.ToString();
TypeCode code = Convert.GetTypeCode(value, ic);
if (pref == PreferredType.LocaleString){
switch (code){
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Single:
case TypeCode.Double: {
double d = ic.ToDouble(null);
return d.ToString(d <= -1e+15 || d >= 1e+15 ? "g" : "n", NumberFormatInfo.CurrentInfo);
}
case TypeCode.Int64: return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
case TypeCode.UInt64: return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
case TypeCode.Decimal: return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo);
}
}
switch (code){
case TypeCode.Empty: return explicitOK ? "undefined" : null;
case TypeCode.Object: return Convert.ToString(Convert.ToPrimitive(value, pref, ref ic), ic);
case TypeCode.DBNull: return explicitOK ? "null" : null;
case TypeCode.Boolean: return ic.ToBoolean(null) ? "true" : "false";
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.Decimal:
case TypeCode.String: return ic.ToString(null);
case TypeCode.DateTime: return Convert.ToString(DateConstructor.ob.Construct(ic.ToDateTime(null)));
case TypeCode.Single:
case TypeCode.Double: return Convert.ToString(ic.ToDouble(null));
}
return null; //Should never get here
}
示例6: DoAddOvf
//.........这里部分代码省略.........
val = f + ic2.ToInt16(null);
type = SystemTypes.Single;
break;
case TypeCode.Int32:
case TypeCode.Int64:
val = f + (double)ic2.ToInt64(null);
type = SystemTypes.Double;
break;
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.UInt16:
val = f + ic2.ToUInt16(null);
type = SystemTypes.Single;
break;
case TypeCode.UInt32:
case TypeCode.UInt64:
val = f + (double)ic2.ToUInt64(null);
type = SystemTypes.Double;
break;
case TypeCode.Single:
case TypeCode.Double:
val = f + ic2.ToDouble(null);
type = SystemTypes.Double;
break;
case TypeCode.Decimal:
val = f + (double)ic2.ToDecimal(null);
type = SystemTypes.Double;
break;
default: return null;
}
break;
case TypeCode.Double:
double d = ic1.ToDouble(null);
switch(code2){
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
val = d + ic2.ToInt64(null);
type = SystemTypes.Double;
break;
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
val = d + ic2.ToUInt64(null);
type = SystemTypes.Double;
break;
case TypeCode.Single:
case TypeCode.Double:
val = d + ic2.ToDouble(null);
type = SystemTypes.Double;
break;
case TypeCode.Decimal:
val = d + (double)ic2.ToDecimal(null);
type = SystemTypes.Double;
break;
default: return null;
}
break;
case TypeCode.Decimal:
decimal dec = ic1.ToDecimal(null);
switch(code2){
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Char:
case TypeCode.Int32:
val = dec + ic2.ToInt32(null);
type = SystemTypes.Decimal;
break;
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
val = dec + ic2.ToInt64(null);
type = SystemTypes.Decimal;
break;
case TypeCode.Decimal:
val = dec + ic2.ToDecimal(null);
type = SystemTypes.Decimal;
break;
default: return null;
}
break;
case TypeCode.String:
string str = ic1.ToString(null);
switch (code2) {
case TypeCode.String:
val = str + ic2.ToString(null);
type = SystemTypes.String;
break;
default: return null;
}
break;
default: return null;
}}
return new Literal(val, type, binaryExpression.SourceContext);
}
示例7: DefaultToType
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
if (targetType==null) {
throw new ArgumentNullException("targetType");
}
Contract.EndContractBlock();
RuntimeType rtTargetType = targetType as RuntimeType;
if (rtTargetType != null)
{
if (value.GetType() == targetType)
{
return value;
}
if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean])
return value.ToBoolean(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Char])
return value.ToChar(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.SByte])
return value.ToSByte(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Byte])
return value.ToByte(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int16])
return value.ToInt16(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16])
return value.ToUInt16(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int32])
return value.ToInt32(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32])
return value.ToUInt32(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int64])
return value.ToInt64(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64])
return value.ToUInt64(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Single])
return value.ToSingle(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Double])
return value.ToDouble(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal])
return value.ToDecimal(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime])
return value.ToDateTime(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.String])
return value.ToString(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Object])
return (Object)value;
// Need to special case Enum because typecode will be underlying type, e.g. Int32
if (rtTargetType == EnumType)
return (Enum)value;
if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
if (rtTargetType == ConvertTypes[(int)TypeCode.Empty])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
}
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName));
}
示例8: DefaultToType
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
{
Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
if (targetType == null)
{
throw new ArgumentNullException("targetType");
}
Contract.EndContractBlock();
if (value.GetType() == targetType)
return value;
if (targetType == CommonRuntimeTypes.Boolean)
return value.ToBoolean(provider);
if (targetType == CommonRuntimeTypes.Char)
return value.ToChar(provider);
if (targetType == CommonRuntimeTypes.SByte)
return value.ToSByte(provider);
if (targetType == CommonRuntimeTypes.Byte)
return value.ToByte(provider);
if (targetType == CommonRuntimeTypes.Int16)
return value.ToInt16(provider);
if (targetType == CommonRuntimeTypes.UInt16)
return value.ToUInt16(provider);
if (targetType == CommonRuntimeTypes.Int32)
return value.ToInt32(provider);
if (targetType == CommonRuntimeTypes.UInt32)
return value.ToUInt32(provider);
if (targetType == CommonRuntimeTypes.Int64)
return value.ToInt64(provider);
if (targetType == CommonRuntimeTypes.UInt64)
return value.ToUInt64(provider);
if (targetType == CommonRuntimeTypes.Single)
return value.ToSingle(provider);
if (targetType == CommonRuntimeTypes.Double)
return value.ToDouble(provider);
if (targetType == CommonRuntimeTypes.Decimal)
return value.ToDecimal(provider);
if (targetType == CommonRuntimeTypes.DateTime)
return value.ToDateTime(provider);
if (targetType == CommonRuntimeTypes.String)
return value.ToString(provider);
if (targetType == CommonRuntimeTypes.Object)
return (Object)value;
if (targetType == CommonRuntimeTypes.Enum)
return (Enum)value;
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, value.GetType().ToString(), targetType.Name));
}
示例9: AssignAmPm
private static void AssignAmPm(IDateTimeResultTO dateTimeResultTo, bool assignAsTime, IConvertible value)
{
string lowerValue = value.ToString(CultureInfo.InvariantCulture).ToLower();
if (lowerValue == "pm" || lowerValue == "p.m" || lowerValue == "p.m.")
{
dateTimeResultTo.AmPm = DateTimeAmPm.pm;
}
else
{
dateTimeResultTo.AmPm = DateTimeAmPm.am;
}
}
示例10: AssignEra
private static void AssignEra(IDateTimeResultTO dateTimeResultTo, bool assignAsTime, IConvertible value)
{
dateTimeResultTo.Era = value.ToString(CultureInfo.InvariantCulture);
}
示例11: ToBoolean
private static bool ToBoolean (object value, bool explicitConversion, IConvertible convertible)
{
TypeCode preferredType = GetTypeCode (value, convertible);
switch (preferredType) {
//undefined & null
case TypeCode.Empty:
case TypeCode.DBNull:
return false;
case TypeCode.Boolean:
return convertible.ToBoolean (null);
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
double d = convertible.ToDouble (null);
return ((d != 0.0) && !double.IsNaN (d));
case TypeCode.String:
string str = convertible.ToString ();
return str.Length != 0;
case TypeCode.Object:
return true;
//TODO datetime find behaviour maybe as a number
case TypeCode.DateTime:
return true;
}
throw new NotImplementedException ();
}
示例12: SetVal
private void SetVal(Field field, IConvertible value, bool isString = false)
{
if (!string.IsNullOrWhiteSpace(field.Name))
this.valList[field.Name] = value;
if (value == null)
content.Append(", NULL");
else if (!isString)
content.Append(", " + value.ToString(CultureInfo.InvariantCulture));
else
{
var val = value.ToString(CultureInfo.InvariantCulture);
if (string.IsNullOrWhiteSpace(val))
content.Append(", NULL");
else
content.Append(", \"" + val.Replace(@"'", @"\'").Replace("\"", "\\\"") + "\"");
}
if (Program.DebugOutput)
content.Append(" /* " + field.Name + " */");
}
示例13: SetResultProperty
/// <summary>
/// Checks the result code of the tests execution and performs the
/// corresponding action.
/// </summary>
/// <param name="resultCode">The result code returned by the Run method of the
/// TestRunnerHelper class.</param>
private void SetResultProperty(IConvertible resultCode)
{
if (!String.IsNullOrEmpty(ResultProperty))
{
Properties[ResultProperty] = resultCode.ToString(CultureInfo.InvariantCulture);
}
}
示例14: JScriptEquals
private static bool JScriptEquals(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects){
if (StrictEquality.JScriptStrictEquals(v1, v2, ic1, ic2, t1, t2, checkForDebuggerObjects))
return true;
if (t2 == TypeCode.Boolean){
v2 = ic2.ToBoolean(null) ? 1 : 0;
ic2 = Convert.GetIConvertible(v2);
return Equality.JScriptEquals(v1, v2, ic1, ic2, t1, TypeCode.Int32, false);
}
switch (t1){
case TypeCode.Empty: return t2 == TypeCode.Empty || t2 == TypeCode.DBNull || (t2 == TypeCode.Object && v2 is Missing);
case TypeCode.Object:
switch (t2){
case TypeCode.Empty:
case TypeCode.DBNull:
return v1 is Missing;
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:
case TypeCode.String:
IConvertible pvic1 = ic1;
Object pv1 = Convert.ToPrimitive(v1, PreferredType.Either, ref pvic1);
if (pvic1 != null && pv1 != v1)
return Equality.JScriptEquals(pv1, v2, pvic1, ic2, pvic1.GetTypeCode(), t2, false);
else
return false;
}
return false;
case TypeCode.DBNull: return t2 == TypeCode.DBNull || t2 == TypeCode.Empty || (t2 == TypeCode.Object && v2 is Missing);
case TypeCode.Boolean:
v1 = ic1.ToBoolean(null) ? 1 : 0;
ic1 = Convert.GetIConvertible(v1);
return Equality.JScriptEquals(v1, v2, ic1, ic2, TypeCode.Int32, t2, false);
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:
if (t2 == TypeCode.Object){
IConvertible pvic2 = ic2;
Object pv2 = Convert.ToPrimitive(v2, PreferredType.Either, ref pvic2);
if (pvic2 != null && pv2 != v2)
return Equality.JScriptEquals(v1, pv2, ic1, pvic2, t1, pvic2.GetTypeCode(), false);
else
return false;
}
if (t2 == TypeCode.String){
if (v1 is Enum) return Convert.ToString(v1).Equals(ic2.ToString(null));
v2 = Convert.ToNumber(v2, ic2);
ic2 = Convert.GetIConvertible(v2);
return StrictEquality.JScriptStrictEquals(v1, v2, ic1, ic2, t1, TypeCode.Double, false);
}
return false;
case TypeCode.DateTime:
if (t2 == TypeCode.Object){
IConvertible pvic2 = ic2;
Object pv2 = Convert.ToPrimitive(v2, PreferredType.Either, ref pvic2);
if (pv2 != null && pv2 != v2)
return StrictEquality.JScriptStrictEquals(v1, pv2, ic1, pvic2, t1, pvic2.GetTypeCode(), false);
}
return false;
case TypeCode.String:
switch (t2){
case TypeCode.Object:{
IConvertible pvic2 = ic2;
Object pv2 = Convert.ToPrimitive(v2, PreferredType.Either, ref pvic2);
if (pvic2 != null && pv2 != v2)
return Equality.JScriptEquals(v1, pv2, ic1, pvic2, t1, pvic2.GetTypeCode(), false);
else
return false;
}
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:
if (v2 is Enum) return Convert.ToString(v2).Equals(ic1.ToString(null));
v1 = Convert.ToNumber(v1, ic1);
ic1 = Convert.GetIConvertible(v1);
//.........这里部分代码省略.........
示例15: EmitConstant
private void EmitConstant(IConvertible ic)
{
this.StackSize++;
TypeCode tc = ic.GetTypeCode();
switch (tc) {
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
long n = ic.ToInt64(null);
switch (n) {
case -1: this.generator.Emit(OperationCode.Ldc_I4_M1); break;
case 0: this.generator.Emit(OperationCode.Ldc_I4_0); break;
case 1: this.generator.Emit(OperationCode.Ldc_I4_1); break;
case 2: this.generator.Emit(OperationCode.Ldc_I4_2); break;
case 3: this.generator.Emit(OperationCode.Ldc_I4_3); break;
case 4: this.generator.Emit(OperationCode.Ldc_I4_4); break;
case 5: this.generator.Emit(OperationCode.Ldc_I4_5); break;
case 6: this.generator.Emit(OperationCode.Ldc_I4_6); break;
case 7: this.generator.Emit(OperationCode.Ldc_I4_7); break;
case 8: this.generator.Emit(OperationCode.Ldc_I4_8); break;
default:
if (sbyte.MinValue <= n && n <= sbyte.MaxValue) {
this.generator.Emit(OperationCode.Ldc_I4_S, (sbyte)n);
} else if (int.MinValue <= n && n <= int.MaxValue ||
n <= uint.MaxValue && (tc == TypeCode.Char || tc == TypeCode.UInt16 || tc == TypeCode.UInt32)) {
if (n == uint.MaxValue)
this.generator.Emit(OperationCode.Ldc_I4_M1);
else
this.generator.Emit(OperationCode.Ldc_I4, (int)n);
} else {
this.generator.Emit(OperationCode.Ldc_I8, n);
tc = TypeCode.Empty; //Suppress conversion to long
}
break;
}
if (tc == TypeCode.Int64)
this.generator.Emit(OperationCode.Conv_I8);
return;
case TypeCode.UInt64:
this.generator.Emit(OperationCode.Ldc_I8, (long)ic.ToUInt64(null));
return;
case TypeCode.Single:
this.generator.Emit(OperationCode.Ldc_R4, ic.ToSingle(null));
return;
case TypeCode.Double:
this.generator.Emit(OperationCode.Ldc_R8, ic.ToDouble(null));
return;
case TypeCode.String:
this.generator.Emit(OperationCode.Ldstr, ic.ToString(null));
return;
case TypeCode.Decimal:
var bits = Decimal.GetBits(ic.ToDecimal(null));
this.generator.Emit(OperationCode.Ldc_I4, bits[0]);
this.generator.Emit(OperationCode.Ldc_I4, bits[1]); this.StackSize++;
this.generator.Emit(OperationCode.Ldc_I4, bits[2]); this.StackSize++;
if (bits[3] >= 0)
this.generator.Emit(OperationCode.Ldc_I4_0);
else
this.generator.Emit(OperationCode.Ldc_I4_1);
this.StackSize++;
int scale = (bits[3]&0x7FFFFF)>>16;
if (scale > 28) scale = 28;
this.generator.Emit(OperationCode.Ldc_I4_S, scale); this.StackSize++;
this.generator.Emit(OperationCode.Newobj, this.DecimalConstructor);
this.StackSize -= 4;
return;
}
}