本文整理汇总了C#中IConvertible.GetTypeCode方法的典型用法代码示例。如果您正苦于以下问题:C# IConvertible.GetTypeCode方法的具体用法?C# IConvertible.GetTypeCode怎么用?C# IConvertible.GetTypeCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConvertible
的用法示例。
在下文中一共展示了IConvertible.GetTypeCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Unify
//extends to Int32/Int64/Decimal/Double
internal static IConvertible Unify (IConvertible o)
{
switch (o.GetTypeCode()) {
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
return (IConvertible)Convert.ChangeType (o, TypeCode.Int32);
case TypeCode.UInt32:
return (IConvertible)Convert.ChangeType (o, TypeCode.Int64);
case TypeCode.UInt64:
return (IConvertible)Convert.ChangeType (o, TypeCode.Decimal);
case TypeCode.Single:
return (IConvertible)Convert.ChangeType (o, TypeCode.Double);
default:
return o;
}
}
示例2: ToSameType
//(note: o1 and o2 must both be of type Int32/Int64/Decimal/Double)
internal static TypeCode ToSameType (ref IConvertible o1, ref IConvertible o2)
{
TypeCode tc1 = o1.GetTypeCode();
TypeCode tc2 = o2.GetTypeCode();
if (tc1 == tc2)
return tc1;
if (tc1 == TypeCode.DBNull || tc2 == TypeCode.DBNull)
return TypeCode.DBNull;
// is it ok to make such assumptions about the order of an enum?
if (tc1 < tc2)
{
o1 = (IConvertible)Convert.ChangeType (o1, tc2);
return tc2;
}
else
{
o2 = (IConvertible)Convert.ChangeType (o2, tc1);
return tc1;
}
}
示例3: GetTypeCode_Primitives
public static void GetTypeCode_Primitives(TypeCode expected, IConvertible convertible)
{
Assert.Equal(expected, convertible.GetTypeCode());
}
示例4: SetOption
//! 设置绘图命令选项(bool/int/float类型的值)
public bool SetOption(string key, IConvertible value)
{
if (key == null || value == null)
{
return false;
}
if (value.GetTypeCode() == TypeCode.String)
{
string str = Convert.ToString(value).ToLower();
int intValue;
double doubleValue;
if (str.CompareTo("true") == 0)
return SetOption(key, true);
if (str.CompareTo("false") == 0)
return SetOption(key, false);
if (int.TryParse(str, out intValue))
return SetOption(key, intValue);
if (double.TryParse(str, out doubleValue))
return SetOption(key, doubleValue);
CoreView.setOptionString(key, Convert.ToString(value));
return true;
}
if (key == "contextActionEnabled") {
View.ContextActionEnabled = Convert.ToBoolean(value);
}
else if (key == "zoomEnabled") {
ZoomEnabled = Convert.ToBoolean(value);
}
else switch (value.GetTypeCode()) {
case TypeCode.Boolean:
CoreView.setOptionBool(key, Convert.ToBoolean(value));
break;
case TypeCode.Int32:
case TypeCode.UInt32:
CoreView.setOptionInt(key, Convert.ToInt32(value));
break;
case TypeCode.Single:
case TypeCode.Double:
CoreView.setOptionFloat(key, Convert.ToSingle(value));
break;
default:
Debug.Assert(false, key);
return false;
}
return true;
}
示例5: EncodeConvertible
protected void EncodeConvertible(IConvertible value, Stream output)
{
output.WriteByte((byte)value.GetTypeCode());
byte[] result;
switch (value.GetTypeCode())
{
// the following encode directly on the stream
case TypeCode.Boolean: output.WriteByte((byte)((bool)value ? 1 : 0)); return;
case TypeCode.Byte: output.WriteByte(value.ToByte(null)); return;
case TypeCode.SByte: output.WriteByte((byte)(value.ToSByte(null) + 128)); return;
case TypeCode.Object:
formatter.Serialize(output, value);
return;
case TypeCode.String: {
long lengthPosition = output.Position;
output.Write(new byte[4], 0, 4);
StreamWriter w = new StreamWriter(output, Encoding.UTF8);
w.Write((string)value);
w.Flush();
long savedPosition = output.Position;
uint payloadLength = (uint)(output.Position - lengthPosition - 4);
output.Position = lengthPosition;
output.Write(DataConverter.Converter.GetBytes(payloadLength), 0, 4);
output.Position = savedPosition;
return;
}
// the following obtain byte arrays which are dumped below
case TypeCode.Char: result = DataConverter.Converter.GetBytes(value.ToChar(null)); break;
case TypeCode.Single: result = DataConverter.Converter.GetBytes(value.ToSingle(null)); break;
case TypeCode.Double: result = DataConverter.Converter.GetBytes(value.ToDouble(null)); break;
case TypeCode.Int16: result = DataConverter.Converter.GetBytes(value.ToInt16(null)); break;
case TypeCode.Int32: result = DataConverter.Converter.GetBytes(value.ToInt32(null)); break;
case TypeCode.Int64: result = DataConverter.Converter.GetBytes(value.ToInt64(null)); break;
case TypeCode.UInt16: result = DataConverter.Converter.GetBytes(value.ToUInt16(null)); break;
case TypeCode.UInt32: result = DataConverter.Converter.GetBytes(value.ToUInt32(null)); break;
case TypeCode.UInt64: result = DataConverter.Converter.GetBytes(value.ToUInt64(null)); break;
case TypeCode.DateTime: result = DataConverter.Converter.GetBytes(((DateTime)value).ToBinary()); break;
default: throw new MarshallingException("Unhandled form of IConvertible: " + value.GetTypeCode());
}
output.Write(result, 0, result.Length);
}
示例6: Negative
internal static IConvertible Negative (IConvertible o)
{
switch (o.GetTypeCode()) {
case TypeCode.Int32:
return -((int)o);
case TypeCode.Int64:
return -((long)o);
case TypeCode.Double:
return -((double)o);
case TypeCode.Decimal:
return -((decimal)o);
default:
return DBNull.Value;
}
}
示例7: WriteIConvertible
public static void WriteIConvertible(IConvertible input)
{
Console.WriteLine("{0}: {1}", input.GetTypeCode(), input.ToString(null).ToLower());
}
示例8: CheckNumericPromotion
public static IConvertible CheckNumericPromotion(IConvertible convertible)
{
if (IsPromotableNumeric(convertible.GetTypeCode()))
return convertible;
throw new InvalidCastException();
}
示例9: 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;
}
}
示例10: VarArgs
/*
public static object [] VarArgs (object [] args, int offset, int n)
{
throw new NotImplementedException ();
}
*/
internal static TypeCode GetTypeCode (object obj, IConvertible convertible)
{
if (obj == null)
return TypeCode.Empty;
else if (convertible == null)
return TypeCode.Object;
else
return convertible.GetTypeCode ();
}
示例11: EmitValue
void EmitValue(IConvertible val)
{
switch (val.GetTypeCode())
{
case TypeCode.UInt64:
g.EmitI8Helper(unchecked((long)val.ToUInt64(null)), false);
break;
case TypeCode.Int64:
g.EmitI8Helper(val.ToInt64(null), true);
break;
case TypeCode.UInt32:
g.EmitI4Helper(unchecked((int)val.ToUInt64(null)));
break;
default:
g.EmitI4Helper(val.ToInt32(null));
break;
}
}
示例12: Diff
static int Diff(IConvertible val1, IConvertible val2)
{
ulong diff;
switch (val1.GetTypeCode())
{
case TypeCode.UInt64:
diff = val2.ToUInt64(null) - val1.ToUInt64(null);
break;
case TypeCode.Int64:
diff = (ulong)(val2.ToInt64(null) - val1.ToInt64(null));
break;
case TypeCode.UInt32:
diff = val2.ToUInt32(null) - val1.ToUInt32(null);
break;
default:
diff = (ulong)(val2.ToInt32(null) - val1.ToInt32(null));
break;
}
if (diff >= int.MaxValue)
return int.MaxValue;
else
return (int)diff;
}
示例13: ExecuteBinaryOperator
private object ExecuteBinaryOperator(IConvertible left, IConvertible right, CodeBinaryOperatorType op)
{
TypeCode typeCode = left.GetTypeCode();
TypeCode code2 = right.GetTypeCode();
TypeCode[] codeArray = new TypeCode[] { TypeCode.Byte, TypeCode.Char, TypeCode.Int16, TypeCode.UInt16, TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 };
int num = -1;
int num2 = -1;
for (int i = 0; i < codeArray.Length; i++)
{
if (typeCode == codeArray[i])
{
num = i;
}
if (code2 == codeArray[i])
{
num2 = i;
}
if ((num != -1) && (num2 != -1))
{
break;
}
}
if ((num == -1) || (num2 == -1))
{
return left;
}
int index = Math.Max(num, num2);
object obj2 = left;
switch (codeArray[index])
{
case TypeCode.Char:
{
char ch = left.ToChar(null);
char ch2 = right.ToChar(null);
if (op != CodeBinaryOperatorType.BitwiseOr)
{
obj2 = ch & ch2;
break;
}
obj2 = ch | ch2;
break;
}
case TypeCode.Byte:
{
byte num5 = left.ToByte(null);
byte num6 = right.ToByte(null);
if (op != CodeBinaryOperatorType.BitwiseOr)
{
obj2 = num5 & num6;
break;
}
obj2 = num5 | num6;
break;
}
case TypeCode.Int16:
{
short num7 = left.ToInt16(null);
short num8 = right.ToInt16(null);
if (op != CodeBinaryOperatorType.BitwiseOr)
{
obj2 = num7 & num8;
break;
}
obj2 = (short) (((ushort) num7) | ((ushort) num8));
break;
}
case TypeCode.UInt16:
{
ushort num9 = left.ToUInt16(null);
ushort num10 = right.ToUInt16(null);
if (op != CodeBinaryOperatorType.BitwiseOr)
{
obj2 = num9 & num10;
break;
}
obj2 = num9 | num10;
break;
}
case TypeCode.Int32:
{
int num11 = left.ToInt32(null);
int num12 = right.ToInt32(null);
if (op != CodeBinaryOperatorType.BitwiseOr)
{
obj2 = num11 & num12;
break;
}
obj2 = num11 | num12;
break;
}
case TypeCode.UInt32:
{
uint num13 = left.ToUInt32(null);
uint num14 = right.ToUInt32(null);
if (op != CodeBinaryOperatorType.BitwiseOr)
{
obj2 = num13 & num14;
break;
}
obj2 = num13 | num14;
//.........这里部分代码省略.........
示例14: GetTypeCode
internal static TypeCode GetTypeCode(Object ob, IConvertible ic){
if (ob == null) return TypeCode.Empty;
if (ic == null) return TypeCode.Object;
return ic.GetTypeCode();
}
示例15: Negate
/// <summary>
/// Negates a <see cref="T:IConvertible"/> value by round tripping through
/// the System.Decimal type.
/// </summary>
/// <param name="value">The value to negate.</param>
/// <param name="formatProvider">The culture information.</param>
/// <returns>The negated value as the original input type.</returns>
private static object Negate(IConvertible value, IFormatProvider formatProvider)
{
TypeCode inputType = value.GetTypeCode();
decimal input = value.ToDecimal(formatProvider);
decimal output = Decimal.Negate(input);
return System.Convert.ChangeType(output, inputType, formatProvider);
}