65.9K
CodeProject 正在变化。 阅读更多。
Home

简单的对象序列化器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.80/5 (11投票s)

2009 年 12 月 8 日

CPOL

3分钟阅读

viewsIcon

38424

downloadIcon

289

使用 .NET 反射和 XML 的简单对象序列化器

引言

您有多少次想通过 TCP/IP 构建一个简单的客户端-服务器应用程序?您有多少次摆弄 .NET Framework 序列化器(XMLSerializerBinarySerializer)以使您的数据对象正确地通过网络发送?不要再挣扎了,看看本文中描述的简单序列化方法。无需再摆弄 MarshalByRefObject、XML 属性类等。

背景

(如果您不熟悉 .NET 反射命名空间,我建议您在阅读本文之前先看看 CodeProject 上关于反射的教程之一。否则,这里展示的代码可能会显得有点晦涩难懂。

Using the Code

好了,让我们开始吧。我们要做的就是开发一个自定义序列化器,将任何给定对象转换为 XML 格式的字符串。在本文中,我将序列化器的功能限制为序列化/反序列化由 Primitives、Enums、Lists(泛型和非泛型)和 Dictionaries(泛型字典、Hashtable)以及这些基本类型的嵌套组合组成的对象。序列化器基本上会递归地循环遍历给定的输入,并将其转换为请求的输出。对于序列化过程,输入将是一个对象实例,该实例的使用仅限于上面指定的类型,输出将是一个包含 XML 文档的字符串实例。对于反序列化过程,输入和输出的角色会相应地互换。

遵循这个思路,对于我们的序列化器类的用户来说,我们只需要两个 public 方法

  • public string Serialize(object input);
  • public object Deserialize(string input);
namespace MySerialization
{
  public class NullValue
  {
    public NullValue()
    {
    }
  }

  public class ObjectSerializer
  {  
    public static string Serialize(object input)
    {
      //Create the xml document instance we are going to work with as well as
      //a main element node
      XmlDocument doc = new XmlDocument();
      XmlNode mainElement = doc.CreateNode(XmlNodeType.Element, "Instance", "");
          
      if (input != null)
      {
        //We need to store the type information of the input object, because we
        //will need it to deserialize this again later. The type information is
        //stored in an attribute of the main element node.
        XmlAttribute att = doc.CreateAttribute("Type");
        att.Value = input.GetType().AssemblyQualifiedName;
        mainElement.Attributes.Append(att);

        //Add the main element node to the xml document.
        doc.AppendChild(mainElement);

        //Here we enter the recursion, passing the xml document instance, the parent
        //xml node of the next recursion level and the parent object of the next 
        //recursion level in as parameters. Because the xml document instance and the 
        //node element are reference types (classes) we don't need a "ref" here.
        //The object could be a value type like a primitive or an enum, but as we are
        //serializing, we will only need read access to the object, so no "ref" needed
        //here either.
        SerializeRecursive(doc, mainElement, input);
        
        //At the end of the recursion, we return the string representation of our xml
        //document instance.
        return doc.InnerXml;
      }
      else
      {
        //During deserialization, we would get in trouble if we were trying to retrieve
        //type information of a null value. Therefore we replace any null values with a
        //dummy instance of a type NullValue.
        return Serialize(new NullValue());
      }
    }
    
    public static object Deserialize(string input)
    {
      //We put the input string back into an xml document instance. This will make life
      //easier in the recursion, because we can loop through the xml node objects.
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(input);
      
      //Retrieve the type of the serialized object, using the Type-Attribute we added 
      //during the serialization. Using this type information, we can create an instance
      //of this type.
      Type mainType = Type.GetType(doc.DocumentElement.Attributes["Type"].Value);
      object result = Activator.CreateInstance(mainType);

      //Any NullValue instances we created during serialization 
      //to replace null values will
      //be transformed back into null values.
      if (result is NullValue)
        result = null;

      if (result != null)
      {
        //Now we get all properties of our object type so we can start recursion.
        PropertyInfo[] properties = mainType.GetProperties();
        foreach (PropertyInfo pi in properties)
        {
          //For this article we exclude indexed properties like type accessors.
          ParameterInfo[] paramInfos = pi.GetIndexParameters();
          if (paramInfos.Length == 0)
            DeserializeRecursive(pi, result, doc.DocumentElement);
        }
      }

      return result;
    }
  }
}

请注意,在本文中,为了简单起见,我们期望应用程序中的每个进程都知道包含我们要序列化/反序列化的类型的程序集。在更通用的上下文中,我们还可以存储包含我们类型的程序集的程序集信息,使用我们可以附加到实例节点的第二个 XmlAttribute。这样,我们可以在反序列化期间加载该程序集,并从该程序集中取出我们序列化对象的类型。我们可以在递归的每个阶段都这样做,但请记住,这会大大增加您的 XML 文件的大小,因此您应该问问自己是否真的需要这种灵活性,或者您是否可以接受所有进程已经知道您的类型的限制。

接下来,我们将看看 SerializeRecursive 方法及其处理的不同情况

  • 图元
  • 枚举类型
  • 列表类型
  • 字典类型
namespace MySerialization
{
  public class NullValue
  {
    public NullValue()
    {
    }
  }

  public class ObjectSerializer
  {  
    public static string Serialize(object input)
    {
      ...
    }
    
    public static object Deserialize(string input)
    {
      ...
    }
    
    //Keep in mind that the xml node is the parent node 
    //and the object is the parent object of the 
    //subobject we are going to serialize in the current recursion level.
    private static void SerializeRecursive
	(XmlDocument doc, XmlNode parentNode, object parentObject)
    {      
      //Handle null value by using a NullValue instance as replacement.
      object o = new NullValue();
      if (parentObject != null)
        o = parentObject;

      Type objType = o.GetType();

      //Case 1: Parent object is a primitive or an enum (or a string)
      if (objType.IsPrimitive || objType.IsEnum)
      {        
        //If our parent object is a primitive or an enum type, 
        //we have reached a leaf in the recursion.
        //A leaf always ends in a concrete value, 
        //which we then store as value of the last parent xml
        //node.
        parentNode.InnerText = o.ToString();        
      }
      else if (objType == typeof(string))
      {
        //The string type has to be handled separately, 
        //because it doesn't count as a primitive.
        parentNode.InnerText = (string)o;
      }
      //Case 2: Parent object is a list type
      else if (typeof(IList).IsAssignableFrom(objType))
      {
        //Unbox the list and loop through all elements, 
        //entering the next recursion level.
        IList tempList = (IList)o;        
        foreach (object i in tempList)
        {
          //For each entry in the list, 
          //we create a child node "ListEntry" and add it below
          //out current parent node.
          XmlNode node = doc.CreateNode(XmlNodeType.Element, "ListEntry", "");
          parentNode.AppendChild(node);

          //Below the ListEntry node, we add another child node 
          //which contains the value of
          //the list item.
          XmlNode valueNode = doc.CreateNode(XmlNodeType.Element, "Value", "");          
          node.AppendChild(valueNode);
          
          //Handle the case when the list item is a null value, 
          //by replacing it with a NullValue
          //instance.
          object item = new NullValue();
          if (i != null)
            item = i;

          //Save the type information of the list item object for deserialization.
          //We add this type info as an attribute, 
          //in the same way we did with the main document
          //node.
          XmlAttribute att = doc.CreateAttribute("Type");
          att.Value = item.GetType().AssemblyQualifiedName;
          valueNode.Attributes.Append(att);
          
          //Enter the next recursion step, using the valueNode 
          //as new parent node and the list item
          //as new parent object.
          SerializeRecursive(doc, valueNode, item);
        }

      }
      //Case 3: Parent object is a dictionary type.
      else if (typeof(IDictionary).IsAssignableFrom(objType))
      {
        //This case works in about the same way as the list type serialization above
        //and should be quite self-explainatory if you understood the list case.
        //The only difference here is that the recursion tree will split into two
        //branches here, one for the key and one for the value of each dictionary
        //entry.
        IDictionary tempDictionary = (IDictionary)o;
        foreach (object key in tempDictionary.Keys)
        {
          XmlNode node = doc.CreateNode(XmlNodeType.Element, "DictionaryEntry", "");
          parentNode.AppendChild(node);

          XmlNode keyNode = doc.CreateNode(XmlNodeType.Element, "Key", "");
          XmlAttribute kAtt = doc.CreateAttribute("Type");
          kAtt.Value = key.GetType().AssemblyQualifiedName;
          keyNode.Attributes.Append(kAtt);

          node.AppendChild(keyNode);
          SerializeRecursive(doc, keyNode, key);

          XmlNode valueNode = doc.CreateNode(XmlNodeType.Element, "Value", "");
          XmlAttribute vAtt = doc.CreateAttribute("Type");

          object entry = new NullValue();
          if (tempDictionary[key] != null)
            entry = tempDictionary[key];

          vAtt.Value = entry.GetType().AssemblyQualifiedName;
          valueNode.Attributes.Append(vAtt);

          node.AppendChild(valueNode);          
          SerializeRecursive(doc, valueNode, entry);
        }
      }
      //Case 4: Parent object is a complex type (class or struct)
      else
      {
        //This case looks similar to what we did in the original Serialize() method.
        //First we get all properties of the current object, then we loop through them,
        //continuing the recursion.
        PropertyInfo[] properties = objType.GetProperties();
        foreach (PropertyInfo pi in properties)
        {
          //Exclude indexed properties
          ParameterInfo[] test = pi.GetIndexParameters();
          if (test.Length == 0)
          {
            XmlNode node = doc.CreateNode(XmlNodeType.Element, pi.Name, "");
            XmlAttribute att = doc.CreateAttribute("Type");
            node.Attributes.Append(att);
            att.Value = pi.PropertyType.AssemblyQualifiedName;
            parentNode.AppendChild(node);
            
            //Get the concrete value of the current property in the parent object
            //so we can continue recursion based on that value object.
            SerializeRecursive(doc, node, pi.GetValue(o, null));
          }
        }
      }
    }
  }
}

如果您完成了这个递归方法并理解了所有情况以及它们的工作方式,那么 DeserializeRecursive() 方法将是不言自明的。您可以在本文附带的示例代码中找到它的注释。

关注点

此示例中的代码旨在演示该想法背后的基本原理。它不包含任何错误处理,也不处理“未处理”的类型。因此,我不建议按原样在项目中使用它。

基本上,这个序列化器可以处理任何只包含原始类型、枚举、字符串、列表和字典的类或结构对象,或者也只包含这些基本类型的类/结构对象。

请记住,有些对象无法序列化,例如 Forms、Controls、Type 或 Assembly 对象。在您的项目中使用这种方法之前,您应该相应地处理这些类型。

历史

  • 版本 1.0:运行 ObjectSerializer 时没有错误处理
© . All rights reserved.