以下是我喜欢的方式。我更喜欢使用一个静态表与一个大开关或反射。
/// <summary>
/// Table that maps TypeCode to it's corresponding Type.
/// </summary>
static IReadOnlyDictionary<TypeCode, Type> TypeCodeToTypeMap = new Dictionary<TypeCode, Type>
{
{ TypeCode.Boolean, typeof(bool) },
{ TypeCode.Byte, typeof(byte) },
{ TypeCode.Char, typeof(char) },
{ TypeCode.DateTime, typeof(DateTime) },
{ TypeCode.DBNull, typeof(DBNull) },
{ TypeCode.Decimal, typeof(decimal) },
{ TypeCode.Double, typeof(double) },
{ TypeCode.Empty, null },
{ TypeCode.Int16, typeof(short) },
{ TypeCode.Int32, typeof(int) },
{ TypeCode.Int64, typeof(long) },
{ TypeCode.Object, typeof(object) },
{ TypeCode.SByte, typeof(sbyte) },
{ TypeCode.Single, typeof(Single) },
{ TypeCode.String, typeof(string) },
{ TypeCode.UInt16, typeof(UInt16) },
{ TypeCode.UInt32, typeof(UInt32) },
{ TypeCode.UInt64, typeof(UInt64) }
};
/// <summary>
/// Convert a TypeCode ordinal into it's corresponding Type instance.
/// </summary>
public static Type ToType(this TypeCode code)
{
Type type = null;
TypeCodeToTypeMap.TryGetValue(code, out type);
return type;
}
给所述输入的一个例子。 – Tejs 2011-05-06 18:50:53
您必须使用colType值的开关大小写。 – 2011-05-06 19:19:55
问题是我不知道什么类型的数字代表什么类型。例如,返回的其中一个数字是72.我怎么知道72型应该代表什么? – user259286 2011-05-06 19:22:53