[英]How do you deserialize an interface to an internal class of another assembly?
Obviously, it is impossible to serialize/deserialize interfaces if the concrete types are not known to the compiler. 显然,如果编译器不知道具体类型,则不可能对接口进行序列化/反序列化。
I have an interface-only assembly MyInterfaces
and an assembly MyImpl
with internal implementations of these interfaces. 我有一个仅接口的程序集MyInterfaces
和一个具有这些接口的内部实现的程序集MyImpl
。 Instantiation looks like this: 实例化如下所示:
MyInterfaces.IFoo foo = MyImpl.FooFactory.CreateFoo();
Here's the factory in assembly MyImpl
: 这是组装MyImpl
的工厂:
IFoo CreateFoo()
{
return new Foo(); // Internal class!
}
And here's the concrete Foo
class in assembly MyImpl
: 这是程序集MyImpl
的具体Foo
类:
[DataContract]
internal class Foo : IFoo
{
[DataMember]
public string Bar
{
get;
set;
}
}
Reading the type of the factory-created IFoo
object allows for serialization of the IFoo
interface: 读取工厂创建的IFoo
对象的类型可以对IFoo
接口进行序列化:
var serializer = new DataContractSerializer(typeof(MyFooContainer),
new Type[] { container.Foo.GetType() });
serializer.WriteObject(writer, container);
When deserializing, the type cannot be deduced since we have no object to look at! 反序列化时,无法推论类型,因为我们没有对象可看!
var serializer = new DataContractSerializer(typeof(MyFooContainer),
new Type[] { /* CANNOT provide other assembly's internal type! */ });
container = (MyFooContainer)serializer.ReadObject(reader);
I was hoping that the serialized XML would be enough for DataContractSerializer
to determine how to create the MyImpl.Foo
object, but no. 我希望序列化的XML足以使DataContractSerializer
确定如何创建MyImpl.Foo
对象,但没有。 It tells me this: 它告诉我:
"Error in line 1 position 269. Element ' http://schemas.datacontract.org/2004/07/MyAssembly:Foo ' contains data from a type that maps to the name ' http://schemas.datacontract.org/2004/07/MyImpl:Foo '. The deserializer has no knowledge..." “第1行第269位错误。元素' http://schemas.datacontract.org/2004/07/MyAssembly:Foo '包含来自映射到名称' http://schemas.datacontract.org/2004的类型的数据/ 07 / MyImpl:Foo '。解串器不知道...”
Q: Using DataContractSerializer
, how do you deserialize an interface to an internal, derived class of another assembly? 问:使用DataContractSerializer
,如何反序列化另一个程序集的内部派生类的接口?
Obviously, it makes sense to have public interfaces with internal implementations. 显然,具有内部实现的公共接口是有意义的。 It also makes sense to allow for serialization. 允许序列化也是有意义的。 Please help! 请帮忙!
More about this particular problem: http://blogs.msdn.com/b/sowmy/archive/2008/10/04/serializing-internal-types-using-xmlserializer.aspx 有关此特定问题的更多信息: http : //blogs.msdn.com/b/sowmy/archive/2008/10/04/serializing-internal-types-using-xmlserializer.aspx
You could let the factory that delivers the instances of your interface have another method to deliver a serializer to serialize/deserialize the objects. 您可以让提供接口实例的工厂使用另一种方法来提供序列化程序以对对象进行序列化/反序列化。 That way, the serializer can know all the internal classes. 这样,序列化程序可以知道所有内部类。
I think you are defeating the purpose of DataContractSerializer by cheating it into serializing non-public data. 我认为您通过欺骗序列化非公共数据而违反了DataContractSerializer的目的。 The design of DataContractSerializer is such that it is meant to serialize publicly visible and sharable data only. DataContractSerializer的设计旨在仅序列化公共可见和可共享的数据。 This helps achieve platform and technology independence by making it possible to share the type information in formats like WSDL. 通过以WSDL之类的格式共享类型信息,可以帮助实现平台和技术独立性。
For the functionality you are looking to achieve - it is better to use BinaryFormatter. 对于您要实现的功能-最好使用BinaryFormatter。 However, then you are stuck with only .NET for de-serialization. 但是,您只能使用.NET进行反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.