• 调用 XmlSerializer.Deserialize 注意:会一直增加程序集。


    当使用XmlSerializer(Type type,Type[] extraTypes)构造函数时:

    调用Deserialize会使 应用程序域的程序集不断增加。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    using System.Reflection;
    using System.Collections;

    namespace ConsoleApplication1
    {
        
    public class Class1
        
    {
            [XmlArrayItem(
    typeof(MyStruct))]
            
    public ArrayList list = new ArrayList();
            
    public void SaveAsXML(Stream s)
            
    {
                XmlSerializer sr 
    = new XmlSerializer(this.GetType());
                sr.Serialize(s, 
    this);

            }

            
    //多次调用没有增加程序集
            public static Class1 Deserialize1(Stream s)
            
    {
                XmlSerializer sr 
    = new XmlSerializer(typeof(Class1));
                
    return (Class1)sr.Deserialize(s);
            }

            
    //多次调用增加了程序集
            public static Class1 Deserialize2(Stream s)
            
    {
                XmlSerializer sr 
    = new XmlSerializer(typeof(Class1), new Type[] typeof(MyStruct) });
                
    return (Class1)sr.Deserialize(s);
            }


            
    public static void Main()
            
    {
                Assembly[] assAry 
    = System.AppDomain.CurrentDomain.GetAssemblies();
                Console.WriteLine( 
    "System assembly count:" + assAry.Length );
                Class1 c 
    = new Class1();
                c.list.Add(
    new MyStruct()) ;
                c.list.Add(
    new MyStruct());
                MemoryStream ms 
    = new System.IO.MemoryStream();
                c.SaveAsXML(ms);
              
                
    for (int i = 0; i < 10; i++)
                
    {
                    ms.Position 
    = 0;
                    c 
    = Class1.Deserialize1(ms);
                }

                assAry 
    = System.AppDomain.CurrentDomain.GetAssemblies();
                Console.WriteLine( 
    "After Deserialize1 count:" + assAry.Length);
                Console.WriteLine(
    "====================================================");
                
    for (int i = 0; i < assAry.Length; i++)
                
    {
                    Console.WriteLine(assAry[i].FullName.Split(
    ',')[0]);
                }

                
    for (int i = 0; i < 10; i++)
                
    {
                    ms.Position 
    = 0;
                    c 
    = Class1.Deserialize2(ms);
                }

                
                assAry 
    = System.AppDomain.CurrentDomain.GetAssemblies();
                Console.WriteLine(
    "====================================================");
                Console.WriteLine(
    "After Deserialize2 count:" + assAry.Length);
                
    for (int i = 0; i < assAry.Length; i++)
                
    {
                    Console.WriteLine(assAry[i].FullName.Split(
    ',')[0]);
                }


            }

            
        }

        
    public class MyStruct
        
    {
            
    public string name;
            
    public int count;
        }

    }

    输出:

    System assembly count:4
    After Deserialize1 count:6
    ====================================================
    mscorlib
    ConsoleApplication1
    System.Xml
    System
    System.Configuration
    fnti6agi
    ====================================================
    After Deserialize2 count:16
    mscorlib
    ConsoleApplication1
    System.Xml
    System
    System.Configuration
    fnti6agi
    ifxcgkts
    yxwhgzrw
    qvh_acbj
    vr_plfcx
    gl9akgfi
    wsq5_vqc
    9gyztvh4
    y-29mn35
    ltvgbal0
    1xkjfbpn
    请按任意键继续. . .

  • 相关阅读:
    leetcode------Clone Graph
    leetcode------Anagrams
    leetcode------Simplify Path
    leetcode------Number of Islands
    leetcode------3Sum Closest
    leetcode------4Sum
    leetcode------3Sum
    leetcode------Binary Tree Right Side View
    leetcode------Word Break
    leetcode------House Robber
  • 原文地址:https://www.cnblogs.com/huigll/p/1993603.html
Copyright © 2020-2023  润新知