C#中如何序列化ArraryList和自定义类型

C#中如何序列化ArraryList和自定义类型,第1张

C#中如何序列化ArraryList和自定义类型,第2张

如何在C#中序列化ArrayList,并在ArrayList中存储自定义类型数据?
请看下面的代码:
-
string localnet _ code;
string pub _ connection;
string local _ connection;
public string Name
{
get { return localnet _ Name;}
set { local net _ name = value;}
}
公共字符串代码
{
get { return localnet _ Code;}
set { local net _ code = value;}
}
公共字符串pub connection
{
get { return pub _ connection;}
set { pub _ connection = value;}
}
公共字符串local connection
{
get { return local _ connection;}
set { local _ connection = value;}
}
公共重写字符串ToString()
{
返回名称;
}
}
考试提示:下面是序列化和反序列化函数:
public string serialize array list(array list)
{
type[]extra = new type[1];
extra[0]= type of(local netinfo);
XML serializer xs = new XML serializer(type of(ArrayList),extra);
memory stream ms = new memory stream();
xmltext writer tw = new xmltext writer(ms,Encoding。默认);
xs。Serialize(tw,al);
tw。close();
返回编码。default . GetString(ms . to array());
}
public ArrayList deserializer ArrayList(string ser questions,Type type,Type[]extra Type)
{
XML serializer xs = new XML serializer(Type,extra Type);
StreamReader Sr = new StreamReader(new memory stream(System。text . encoding . default . getbytes(ser questions)),System。text . coding . default);
return (ArrayList)xs。反序列化(Sr);
}
测试代码:
ArrayList al = new ArrayList();
local netinfo = new local netinfo();
信息。Code = " 11
信息。Name = " name1
信息。PubConnection = " ewere[br/]al。添加(信息);
local netinfo info 2 = new local netinfo();
info2。Code = " 112
info2。Name = " name12
info2。PubConnection = " ewere2[br/]al。添加(信息2);
//序列化..............
String S2 = SerializeArrayList(Al);
Type[] extra2 =新类型[1];
extra 2[0]= type of(local netinfo);
//反序列化...........
ArrayList la = Deserialize ArrayList(S2,类型为(ArrayList),extra 2);
需要说明的是,这里反序列化时使用的是Type[],这是关键!

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » C#中如何序列化ArraryList和自定义类型

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情