说我有以下的字典,像这样:这是为什么可空类型抛出一个异常
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