[英]Can I provide custom serialization for XmlSerializer without implementing IXmlSerializable?
We're using XmlSerializer
, and I want to provide custom serialization for certain classes. 我们正在使用
XmlSerializer
,我想为某些类提供自定义序列化。 However, I don't always have the ability to modify the source code of the class in question, otherwise I could just make it implement IXmlSerializable
. 但是,我并不总是能够修改有问题的类的源代码,否则我可以让它实现
IXmlSerializable
。 Is there any way to do this? 有没有办法做到这一点?
Here's a simple example of the proxy deserialize helper: 这是代理反序列化帮助器的一个简单示例:
Given a type that we cannot directly control serialization of at the class level: 给定一种我们无法直接控制类级别序列化的类型:
public sealed class Class //contrived example
{
public string Property {get;set;}
}
And the xml we need to deserialize with: 我们需要反序列化的xml:
<Class>
<Property>Value</Property>
</Class>
You could create a proxy type to manually process the deserialization process of the target type like so: 您可以创建一个代理类型来手动处理目标类型的反序列化过程,如下所示:
[XmlRoot("Class")] // <-- Very important
public sealed class ClassSerializerProxy : IXmlSerializable
{
public Class ClassValue {get;set;}
public System.Xml.Schema.XmlSchema GetSchema(){return null;}
public void WriteXml(System.Xml.XmlWriter writer){}
public void ReadXml(System.Xml.XmlReader reader)
{
var x = XElement.ReadFrom(reader) as XElement;
this.ClassValue = new Class();
//again this is a simple contrived example
this.ClassValue.Property = x.XPathSelectElement("Property").Value;
}
}
Usage is: 用法是:
void Main()
{
// get the xml value somehow
var xdoc= XDocument.Parse(@"<Class><Property>Value</Property></Class>");
// deserialize the xml into the proxy type
var proxy = Deserialize<ClassSerializerProxy>(xdoc);
// read the resulting value
var value = proxy.ClassValue;
}
public object Deserialize(XDocument xmlDocument, Type DeserializeToType)
{
XmlSerializer xmlSerializer = new XmlSerializer(DeserializeToType);
using (XmlReader reader = xmlDocument.CreateReader())
return xmlSerializer.Deserialize(reader);
}
Now throw in some generics and an extension method, and we can clean the call site up a bit for a final (EXCEPT EXCEPTION HANDLING) version: 现在抛出一些泛型和扩展方法,我们可以为最终(EXCEPT EXCEPTION HANDLING)版本清理调用站点:
Usage: 用法:
void Main()
{
var xml = @"<Class><Property>Value</Property></Class>";
var value = xml.DeserializeWithProxy<ClassSerializerProxy,Class>();
value.Dump();
}
Your instance type: 您的实例类型:
public sealed class Class
{
public string Property {get;set;}
}
An interface that proxy types must implement 代理类型必须实现的接口
public interface ISerializerProxy<TInstanceType> where TInstanceType : class
{
TInstanceType Value { get; }
}
The example proxy now implements the new interface 示例代理现在实现了新接口
[XmlRoot("Class")]
public sealed class ClassSerializerProxy : IXmlSerializable, ISerializerProxy<Class>
{
public Class Value {get;set;}
public System.Xml.Schema.XmlSchema GetSchema(){return null;}
public void WriteXml(System.Xml.XmlWriter writer){}
public void ReadXml(System.Xml.XmlReader reader)
{
var x = XElement.ReadFrom(reader) as XElement;
this.Value = new Class();
this.Value.Property = x.XPathSelectElement("Property").Value;
}
}
The deserialization method is now an extension method on string
and can be used with any proxy type. 反序列化方法现在是
string
的扩展方法,可以与任何代理类型一起使用。
public static class ExtensionMethods
{
public static TInstanceType DeserializeWithProxy<TProxyType,TInstanceType>(this string xml)
where TProxyType : ISerializerProxy<TInstanceType>
where TInstanceType : class
{
using (XmlReader reader = XDocument.Parse(xml).CreateReader())
{
var xmlSerializer = new XmlSerializer(typeof(TProxyType));
return (xmlSerializer.Deserialize(reader) as ISerializerProxy<TInstanceType>).Value;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.