如何使用XmlSerializer序列化System.Type对象? [英] how to serialize a System.Type object with XmlSerializer?

查看:39
本文介绍了如何使用XmlSerializer序列化System.Type对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这种类型:

  [DataContract]
  public class EntityId
  {
    [DataMember(Order = 1)]
    public string IdAsString { get; set; }
    [DataMember(Order = 2)]
    public Type Type { get; set; }
  }

我为此创建了一个Xml序列化程序程序集.但是,尝试对其进行序列化会产生异常:

I created an Xml serializer assembly for it. However, trying to serialize it yeilds an exception:

System.InvalidOperationException occurred
  Message=The type NC.DTO.FlowFolder was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
  Source=NC.DTO.XmlSerializers
  StackTrace:
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(String n, String ns, Type o, Boolean isNullable, Boolean needType)
  InnerException: 

NC.DTO.FlowFolder 实际上是 typeof(NC.DTO.FlowFolder)的字符串表示形式-类型为 NC.DTO.FlowFolder ,似乎无法序列化的是 Type 对象本身.

Where NC.DTO.FlowFolder is actually the string representation of typeof(NC.DTO.FlowFolder) - the type NC.DTO.FlowFolder is known to the Xml serializer, what it seems to fail to serialize is the Type object itself.

反射器显示失败的方法内容:

The reflector reveals the failing method contents:

private void Write3_Type(string n, string ns, Type o, bool isNullable, bool needType)
{
    if (o == null)
    {
        if (isNullable)
        {
            base.WriteNullTagLiteral(n, ns);
        }
    }
    else if (!needType && (o.GetType() != typeof(Type)))
    {
        throw base.CreateUnknownTypeException(o);
    }
}

注意 throw 语句.

堆栈跟踪为:

NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(string n, string ns, System.Type o, bool isNullable, bool needType) + 0xda bytes    
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write11_EntityId(string n, string ns, NC.DTO.EntityId o, bool isNullable, bool needType) + 0x2a8 bytes  
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write14_FlowFolder(string n, string ns, NC.DTO.FlowFolder o, bool isNullable, bool needType) + 0x36b bytes  
NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write42_FlowFolder(object o) + 0xc9 bytes   
[Native to Managed Transition]  
System.Xml.dll!System.Xml.Serialization.TempAssembly.InvokeWriter(System.Xml.Serialization.XmlMapping mapping, System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 342 + 0xb9 bytes    C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 676 + 0xdf bytes   C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle) Line 646   C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces) Line 640 C#
System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o) Line 616  C#

我的问题是如何使Xml序列化程序高兴地序列化和反序列化 EntityId 对象?

My question is how can I make Xml serializer happy to serialize and deserialize EntityId objects?

谢谢.

PS

如果有人知道备用的Xml序列化库,那么将XML序列化为Newtonsoft.Json转换为JSON序列化-请请分享.

If someone knows an alternative Xml serialization library, something that would be to XML serialization as Newtonsoft.Json to JSON serialization - please please please share.

推荐答案

xml通常旨在独立于平台,因此为何不起作用.您可以尝试:

xml is usually intended to be platform independent, hence why that isn't working. You could try:

[XmlIgnore]
public Type Type { get; set; }
[XmlElement("Type")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string AssemblyQualifiedTypeName
{
    get { return Type == null ? null : Type.AssemblyQualifiedName; }
    set { Type = string.IsNullOrEmpty(value) ? null : Type.GetType(value); }
}

还请注意,如果您使用的是 XmlSerializer ,则数据合同属性无效.还请注意,这将使您与.NET以及可能的特定版本相关联.

Note also that the data-contract attributes have no effect if you are using XmlSerializer. Note also that this will tie you to .NET and possibly particular versions etc.

这篇关于如何使用XmlSerializer序列化System.Type对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆