所以我有键和值的字典,其中值是一个对象,当在 .NET Core 和 .NET 之间交换二进制数据时,反之亦然,序列化失败
System.Runtime.Serialization.SerializationException: 'Unable to load type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.IDictionary`2[[SharedLib.HostPropertyType, DataInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] required for deserialization.'
这很有意义,因为目标类型位于另一个库中,问题是是否有办法在不创建自定义类型的情况下避免这种情况?
谢谢。
小智 6
虽然这是您尚未指定的内容,但看起来您正在使用BinaryFormatter. 这是一个使用起来很尴尬的格式化程序,因为它可能对它使用的类型来自哪些程序集非常挑剔,看起来你已经遇到了。
但是,您应该能够通过设置属性来指定格式化程序如何绑定其类型Binder。
如果您查看上面的 MS Docs 链接,您应该能够看到您需要做什么。我也复制到这里了:
sealed class Version1ToVersion2DeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type typeToDeserialize = null;
// For each assemblyName/typeName that you want to deserialize to
// a different type, set typeToDeserialize to the desired type.
String assemVer1 = Assembly.GetExecutingAssembly().FullName;
String typeVer1 = "Version1Type";
if (assemblyName == assemVer1 && typeName == typeVer1)
{
// To use a type from a different assembly version,
// change the version number.
// To do this, uncomment the following line of code.
// assemblyName = assemblyName.Replace("1.0.0.0", "2.0.0.0");
// To use a different type from the same assembly,
// change the type name.
typeName = "Version2Type";
}
// The following line of code returns the type.
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
return typeToDeserialize;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为您想要做的一般想法本质上是告诉格式化程序可以使用 IDictionary 的 .net 核心版本,方法是将它正在寻找的程序集与您正在使用的程序集进行交换。
由于您没有具体说明您使用的 .net core 和 fraemwork 的确切版本,我真的不能再具体了,但这应该对您有所帮助。
| 归档时间: |
|
| 查看次数: |
166 次 |
| 最近记录: |