当使用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;
}
}
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
请按任意键继续. . .