zoukankan      html  css  js  c++  java
  • 简单对象的序列化

    源代码:/Files/zhuqil/ObjectSerializer.zip

    导言

          有多少次你仅仅想通过TCP/IP 协议来构建一个简单的客户端/服务器程序,多少次,使用 .NET Framework 序列化(XMLSerializer 或者BinarySerializer)通过让您的数据对象通过网络发送正确,被你搞得一团糟,不用争辩,在这篇文章中你看见简单的序列化的介绍。在也不会被MarshalByRefObject和XML的一些属性弄得一团糟。

    背景:

      如果你不熟悉.NET Reflection 命名空间,在读这篇文章之前,我建议你看看一些关于反射的教程,否则这些代码看起来比较神秘和困惑。

    使用代码:

       好,现在我们开始,我们将来开发一个自定义的能将任何给出的对象转换成一段XML格式的字符串的序列化类。这篇文章中,我对序列化和反序列化对象做了一些限制,这些对象只包括:基元(Primitives),枚举(Enums),Lists(通用和非通用)和字典(通用dictionary,Hashtable)类型,以及这些基本类型的嵌套组合。基本上,该序列化是通过循环递归给定的输入值然后将它转换成要求的输出值.对于序列化过程中,输入值将是一个是仅限于以上述的类型对象实例,输出值将是XML一个字符串实例。对于在反序列化的过程中,输入和输出的角色做相应交换。

        根据这个思路,提供给用户的序列化类,我们需要有两个公有的方法:

    • 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 方法,看他如何处理不同的情况。

    • Primitives
    • Enum Types
    • List Types
    • Dictionary Types
      代码
      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() 方法不言自明。你能找到注释在这篇文章附加的代码里面.

      兴趣点:

      例子的代码只为了说明基本的一些观点. 它没有包含任何的错误处理,它不能处理 "unhandled" 的类型,因此我建议不要将它使用在项目里面。

      节本上这个序列化类能处理任何一个只包含 primitives, enums, strings, Lists 和 Dictionaries 的的对象

      请记住,有一些对象是不能序列化的,例如:Forms, Controls, Type 或者 Assembly 对象,如果你想在你的项目中使用这种方法,你应该相应的处理这些类型。

      翻译:http://www.codeproject.com/KB/cs/ObjectSerializer.aspx



    (全文完)


    以下为广告部分

    您部署的HTTPS网站安全吗?

    如果您想看下您的网站HTTPS部署的是否安全,花1分钟时间来 myssl.com 检测以下吧。让您的HTTPS网站变得更安全!

    SSL检测评估

    快速了解HTTPS网站安全情况。

    安全评级(A+、A、A-...)、行业合规检测、证书信息查看、证书链信息以及补完、服务器套件信息、证书兼容性检测等。

    SSL证书工具

    安装部署SSL证书变得更方便。

    SSL证书内容查看、SSL证书格式转换、CSR在线生成、SSL私钥加解密、CAA检测等。

    SSL漏洞检测

    让服务器远离SSL证书漏洞侵扰

    TLS ROBOT漏洞检测、心血漏洞检测、FREAK Attack漏洞检测、SSL Poodle漏洞检测、CCS注入漏洞检测。

  • 相关阅读:
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十四):权限控制(Shiro 注解)
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十三):配置中心(Config、Bus)
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十二):链路追踪(Sleuth、Zipkin)
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十一):服务网关(Zuul)
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十):服务熔断(Hystrix、Turbine)
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十九):服务消费(Ribbon、Feign)
    Spring Boot + Mybatis 实现动态数据源
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十八):注册中心(Spring Cloud Consul)
    Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十七):登录验证码实现(Captcha)
    Spring Boot + Spring Cloud 构建微服务系统(十):配置中心(Spring Cloud Bus)
  • 原文地址:https://www.cnblogs.com/zhuqil/p/1620975.html
Copyright © 2011-2022 走看看