2011-11-14 48 views
1

我想反序列化一个由JSON.Net 4.0 r3本身序列化的JSON字符串。序列化和反序列化的设置相同。会出现以下情况例外:BindingFailure通过JSON.Net 4.0反序列化r3

找不到类型 'System.Collections.Generic.List`1 [[System.Drawing.PointF, System.Drawing中]]' 在组件“” mscorlib程序,版本= 4.0 .0.0, 文化=中性公钥= b77a5c561934e089" ”

接通后‘停止时BindingFailure抛出’,我看到的问题是在以下的部分(JSON.Net源代码,DefaultSerializationBinder.cs) :

if (assemblyName != null) 
    { 
    Assembly assembly; 

#if !SILVERLIGHT && !PocketPC 
    // look, I don't like using obsolete methods as much as you do but this is the only way 
    // Assembly.Load won't check the GAC for a partial name 
#pragma warning disable 618,612 
    assembly = Assembly.LoadWithPartialName(assemblyName); 
#pragma warning restore 618,612 
#else 
    assembly = Assembly.Load(assemblyName); 
#endif 

    if (assembly == null) 
     throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName)); 

    Type type = assembly.GetType(typeName); // BindingFailure here 
    if (type == null) 
     throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName)); 
    } 

在BindingFailure的点示出的错误是

与显示名称“System.Drawing中”的组件未能在 “LoadFrom”加载结合的AppDomain的上下文ID为1的 的原因失败是:System.IO.FileNotFoundException:无法加载文件 或程序集“System.Drawing”或它的一个依赖项。系统 找不到指定的文件。

操作系统是Windows 7,64位。我正在使用Visual Studio 2010并将此应用程序定位到Framework v4。目标不是Silverlight或PocketPC。

为什么在这种情况下无法加载“System.Drawing”?我应该从哪里开始调查这是JSON.Net问题还是我的Framework 4.0安装问题?

在此先感谢。

回答

0

原来这个问题与JSON.Net有关。我用下面的设置

jsonSerializerSettings = new JsonSerializerSettings 
{ 
    TypeNameHandling = TypeNameHandling.All 
}; 

这是因为在后一个建议(即参与反序列化泛型集合类似清单)。有人建议完整的类型信息可以帮助解决JSon.Net有关“无法实例化抽象基类”错误消息的错误。

如果我将其更改为

jsonSerializerSettings = new JsonSerializerSettings 
{ 
    TypeNameHandling = TypeNameHandling.Auto 
}; 

它工作正常。有趣的是,一些类型信息没有被发射,我没有得到BindingFailure,反序列化工作正常。我不知道为什么它首先不适用于“自动”。