以下是我喜歡的方式。我更喜歡使用一個靜態表與一個大開關或反射。
/// <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