问题是,如果反序列化对象的类型是"C,A“,但是当我编写代码时:
var c = GetDeserializedObject() as C;C将为null,因为GetDeserializedObject() is C返回false。
问题:
知道我该怎么做吗?在程序集A中序列化的程序集B中使用类C。
备注:
我使用这段代码序列化我的对象:
var serialized = MessagePackSerializer.Typeless.Serialize(this);
File.WriteAllBytes(outputFilePath, serialized);而这个反序列化代码是:
MessagePackSerializer.Typeless.Deserialize(File.ReadAllBytes(inputFilePath)) as C;附加评论:
我不能使用BinaryFormatter,因为C类包含一些类型不可序列化的属性
我也尝试使用Newtonsoft Json序列化程序,但在反序列化过程中无法读取数据。如果我用类型序列化它,那么就会出现类型不匹配。如果我在没有类型的情况下序列化它们,在某些情况下,系统不能实例化接口类型或抽象类类型。因为我的课程是这样的:
class C {
IMyInterface i;
}
class MyClass : IMyInterface { }我就是这样用的:
var c = new C {
i = new MyClass()
};发布于 2019-03-04 06:11:04
这是如何反序列化:
MessagePack.Formatters.TypelessFormatter.BindToType = typeName =>
{
var typeWithoutAssemblyName = typeName.Split(',').FirstOrDefault();
return Type.GetType(typeWithoutAssemblyName ?? typeName, false);
};
return MessagePackSerializer.Typeless.Deserialize(File.ReadAllBytes(inputFilePath)) as C;发布于 2019-03-02 22:13:47
我建议您在启用TypeNameHandlig标志的情况下使用JSON序列化器和反序列化程序,这样它就可以处理C类中的接口序列化了
var indented = Formatting.Indented; var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }; string serialized = JsonConvert.SerializeObject(wizardConf, indented, settings);你可以看到细节这里
https://stackoverflow.com/questions/54963004
复制相似问题