• 从 sbyte 到 byte、ushort、uint、ulong 或 char。
• 从 byte 到 sbyte 和 char。
• 从 short 到 sbyte、byte、ushort、uint、ulong 或 char。
• 从 ushort 到 sbyte、byte、short 或 char。
• 从 int 到 sbyte、byte、short、ushort、uint、ulong 或 char。
• 从 uint 到 sbyte、byte、short、ushort、int 或 char。
• 从 long 到 sbyte、byte、short、ushort、int、uint、ulong 或 char。
• 从 ulong 到 sbyte、byte、short、ushort、int、uint、long 或 char。
• 从 char 到 sbyte、byte 或 short。
• 从 float 到 sbyte、byte、short、ushort、int、uint、long、ulong、char 或 decimal。
• 从 double 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、float 或 decimal。
• 从 decimal 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、float 或 double。一个 double 要装换成 (sbyte|byte|short|.....) 要处理异常等东西double 先装换 最小的值类型,最小的值类型不合适的话,就转第二小,.第三小......这样
给个详细的DEMO
• 从 byte 到 sbyte 和 char。
• 从 short 到 sbyte、byte、ushort、uint、ulong 或 char。
• 从 ushort 到 sbyte、byte、short 或 char。
• 从 int 到 sbyte、byte、short、ushort、uint、ulong 或 char。
• 从 uint 到 sbyte、byte、short、ushort、int 或 char。
• 从 long 到 sbyte、byte、short、ushort、int、uint、ulong 或 char。
• 从 ulong 到 sbyte、byte、short、ushort、int、uint、long 或 char。
• 从 char 到 sbyte、byte 或 short。
• 从 float 到 sbyte、byte、short、ushort、int、uint、long、ulong、char 或 decimal。
• 从 double 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、float 或 decimal。
• 从 decimal 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、float 或 double。一个 double 要装换成 (sbyte|byte|short|.....) 要处理异常等东西double 先装换 最小的值类型,最小的值类型不合适的话,就转第二小,.第三小......这样
给个详细的DEMO
{
static void Main()
{
foreach (double d in new double[]{1.2,128,257,32768.5})
{
Console.WriteLine("{0} {1}", d, Convert(d));
}
}
static Type Convert(double x)
{
try
{
sbyte y = checked((sbyte)x);
return typeof(sbyte);
}
catch
{
try
{
byte y = checked((byte)x);
return typeof(byte);
}
catch
{
try
{
short y = checked((short)x);
return typeof(short);
}
catch
{
return null;
}
}
}
}
}
/* 程序输出:
1.2 System.SByte
128 System.Byte
257 System.Int16
32768.5
*/
{
static void Main()
{
foreach (double d in new double[]{1.2,128,257,32768.5})
{
Console.WriteLine("{0} {1}", d, Convert(d));
}
}
static Type Convert(double x)
{
checked
{
try
{
sbyte y = (sbyte)x;
return typeof(sbyte);
}
catch (System.OverflowException)
{
try
{
byte y = (byte)x;
return typeof(byte);
}
catch (System.OverflowException)
{
try
{
short y = (short)x;
return typeof(short);
}
catch (System.OverflowException)
{
return null;
}
}
}
}
}
}
/* 程序输出:
1.2 System.SByte
128 System.Byte
257 System.Int16
32768.5
*/
{
public Type type;
public sbyte sb;
public byte by;
public short sh;
public ushort us;
public int @in;
public uint ui;
public long lo;
public ulong ul;
public char ch;
public float fl;
public decimal de;
}class Program
{
static void Main()
{
foreach (double x in new double[]{0,-1,1.2,128,257,-32768.5,123456789,double.MinValue,double.MaxValue,double.NaN})
{
Numeric y = Convert(x);
Console.WriteLine("{0}\t{1}", x, y.type ?? Type.Missing);
}
}
static Numeric Convert(double x)
{
Numeric n = new Numeric();
n.type = null;
checked
{
try
{
n. de = (decimal)x; n.type = typeof(decimal);
n. fl = (float )x; n.type = typeof(float );
n. ch = (char )x; n.type = typeof(char );
n. ul = (ulong )x; n.type = typeof(ulong );
n. lo = (long )x; n.type = typeof(long );
n. ui = (uint )x; n.type = typeof(uint );
n.@in = (int )x; n.type = typeof(int );
n. us = (ushort )x; n.type = typeof(ushort );
n. sh = (short )x; n.type = typeof(short );
n. by = (byte )x; n.type = typeof(byte );
n. sb = (sbyte )x; n.type = typeof(sbyte );
}
catch (System.OverflowException)
{
try
{
n. sb = (sbyte )x; n.type = typeof(sbyte );
}
catch (System.OverflowException){}
}
}
return n;
}
}
/* 程序输出:
0 System.SByte
-1 System.SByte
1.2 System.SByte
128 System.Byte
257 System.Int16
-32768.5 System.Single
123456789 System.Single
-1.79769313486232E+308 System.Reflection.Missing
1.79769313486232E+308 System.Reflection.Missing
非数字 System.Reflection.Missing
*/