序列化和恢复未知类 [英] Serializing and restoring an unknown class
问题描述
一个基础项目包含一个抽象基类 Foo.在单独的客户端项目中,有实现该基类的类.
我想通过调用基类上的一些方法来序列化和恢复具体类的实例:
//在基础项目中:公共抽象类 Foo{抽象无效保存(字符串路径);抽象 Foo Load(字符串路径);}
可以假设在反序列化时,所有需要的类都存在.如果可能的话,序列化应该在 XML 中完成.使基类实现 IXmlSerializable 是可能的.
我有点卡在这里.如果我对事情的理解是正确的,那么这只能通过向每个实现类的基类添加 [XmlInclude(typeof(UnknownClass))]
来实现 - 但实现类是未知的!>
有没有办法做到这一点?我没有反射的经验,但我也欢迎使用它的答案.
问题是反序列化.只是序列化会很容易.:-)
您也可以在创建 XmlSerializer
时执行此操作,方法是在构造函数中提供其他详细信息.请注意,它不会重复使用此类模型,因此您需要将 XmlSerializer
配置一次(在应用程序启动时,从配置中),并重复使用它...注意更多可以使用 XmlAttributeOverrides
重载进行自定义...
使用系统;使用 System.Collections.Generic;使用 System.IO;使用 System.Xml.Serialization;静态类程序{静态只读 XmlSerializer ser;静态程序(){列表<类型>extraTypes = new List();//TODO: 读取配置,或使用反射来//查看所有程序集extraTypes.Add(typeof(Bar));ser = new XmlSerializer(typeof(Foo), extraTypes.ToArray());}静态无效主(){foo foo = new Bar();MemoryStream ms = new MemoryStream();ser.Serialize(ms, foo);ms.Position = 0;Foo clone = (Foo)ser.Deserialize(ms);Console.WriteLine(clone.GetType());}}公共抽象类 Foo { }公共类酒吧:Foo {}
A base project contains an abstract base class Foo. In separate client projects, there are classes implementing that base class.
I'd like to serialize and restore an instance of a concrete class by calling some method on the base class:
// In the base project:
public abstract class Foo
{
abstract void Save (string path);
abstract Foo Load (string path);
}
It can be assumed that at the time of deserialization, all needed classes are present. If possible in any way, the serialization should be done in XML. Making the base class implement IXmlSerializable is possible.
I'm a bit stuck here. If my understanding of things is correct, then this is only possible by adding an [XmlInclude(typeof(UnknownClass))]
to the base class for every implementing class - but the implementing classes are unknown!
Is there a way to do this? I've got no experience with reflection, but i also welcome answers using it.
Edit: The problem is Deserializing. Just serializing would be kind of easy. :-)
You can also do this at the point of creating an XmlSerializer
, by providing the additional details in the constructor. Note that it doesn't re-use such models, so you'd want to configure the XmlSerializer
once (at app startup, from configuration), and re-use it repeatedly... note many more customizations are possible with the XmlAttributeOverrides
overload...
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class Program
{
static readonly XmlSerializer ser;
static Program()
{
List<Type> extraTypes = new List<Type>();
// TODO: read config, or use reflection to
// look at all assemblies
extraTypes.Add(typeof(Bar));
ser = new XmlSerializer(typeof(Foo), extraTypes.ToArray());
}
static void Main()
{
Foo foo = new Bar();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, foo);
ms.Position = 0;
Foo clone = (Foo)ser.Deserialize(ms);
Console.WriteLine(clone.GetType());
}
}
public abstract class Foo { }
public class Bar : Foo {}
这篇关于序列化和恢复未知类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!