简体   繁体   English

您如何反序列化另一个程序集的内部类的接口?

[英]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;
    }
}

Serialization is trivial 序列化是微不足道的

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);

Deserialization impossible? 反序列化不可能吗?

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.

相关问题 如何从另一个程序集中的内部接口派生 - How to derive from an internal interface in another assembly 如何使用“InternalsVisibleTo”从 XAML 访问另一个程序集中的内部类转换器 - How to access to Internal class converter in another assembly from XAML with "InternalsVisibleTo" 如何从另一个程序集访问内部静态类? - How can I access to an internal static class from another assembly? 如何通过接口组装从另一个模块中的一个模块实例化一个类? - How to instantiate a class from one module in another via interface assembly? XMLSerializer-如何反序列化 - XMLSerializer - how do you deserialize 你如何“覆盖”C#中的内部类? - How do you “override” an Internal Class in C#? 用接口反序列化一个类 - Deserialize a class with interface InternalsVisibleTo-如何防止某人创建具有相同名称的程序集并访问内部类或方法? - InternalsVisibleTo - How do you prevent someone from creating an assembly with same name and get access to internal classes or methods? 如何序列化包含接口的不可修改的类? - How do you serialize an unmodifiable class that contains an interface? 如何在将参数传递给基类的同时反序列化派生类 - How do you deserialize derived class while passing parameters to base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM