說我有以下的字典,像這樣:這是爲什麼可空類型拋出一個異常
public static Dictionary<Type, string> nullableTypeToStringMap = new Dictionary<Type, string>()
{
{typeof(bool?) , "bool?"},
{typeof(byte?) , "byte?"},
{typeof(sbyte?) , "sbyte?"},
{typeof(char?) , "char?"},
{typeof(decimal?) , "decimal?"},
{typeof(double?) , "double?"},
{typeof(float?) , "float?"},
{typeof(int?) , "int?"},
{typeof(uint?) , "uint?"},
{typeof(long?) , "long?"},
{typeof(ulong?) , "ulong?"},
{typeof(short?) , "short?"},
{typeof(ushort?) , "ushort?"}
};
,並說我有執行這一行:
nullableTypeToStringMap [typeof(int?)];
我得到以下異常:The type initializer for 'DatabaseUtils.Utils.TypeMap' threw an exception.
但是,如果我執行此行:
nullableTypeToStringMap [typeof(int)];
它工作正常。任何想法爲什麼可爲空類型導致我的問題?
有什麼的InnerException? – SLaks 2011-12-21 17:06:17
TypeMap的類型初始化時是否還有其他事情發生?任何其他靜態字段或非空cctor? – AakashM 2011-12-21 17:07:18
我猜測那裏int?你是不是初始化它爲空,所以如果你是int,它怎麼會是空的?敏= NULL然後檢查我打賭它不會出錯的類型.. – MethodMan 2011-12-21 17:07:34