六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

對于序列化-開心,轉來一篇

[摘要]建立一個Serialization類,包括兩個靜態方法Save和Get。見程序中的必須注意,對要序列化的類,以及其包括的其他結構和類,都必須用[Serializable]或者[NonSerialized]來標記。NET Serialization By Vyacheslav Biktagirov....
建立一個Serialization類,包括兩個靜態方法Save和Get。見程序中的
必須注意,對要序列化的類,以及其包括的其他結構和類,都必須用[Serializable]或者[NonSerialized]來標記。

NET Serialization By Vyacheslav Biktagirov
.NET enters in our life. So, we must at least learn what about new technoloy of. I think, that XML persistance is one of keys that make .NET belling so good.. OK. So what about persisting of? Imagine we have a class value. In C# termines, like that:
public class NiceClass
{
public long SomeLong=10;
private string str="Now nothing";
public int SomeInt=15;
public string SomeString
{
get
{
return str;
}
set
{
str=value;
}
}
public NiceClass()
{
}
}

Okay, it has some members and one property. Now, we get instance of such class:
NiceClass x=new NiceClass();
Now we work with it and, after work, want to "save" the class to some stream and "send" it to inother application. How we do such a thing? There is special class, named BinaryFormatter, that can take class instance, "save" it in binary format. Than, we can send it to any stream, and restore for our enjoy. But, this class needs some info about our class, like what members we want to store and what not. How we can did it? There is special C# mechanism for class self-describing named "custom properties". Let's do it:

[Serializable]// says class to be serializable
public class NiceClass
{
public long SomeLong=10;
private string str="Now nothing";
[NonSerialized] public int SomeInt=15; // this member is not for serialization
public string SomeString
{
get
{
return str;
}
set
{
str=value;
}
}
public NiceClass()
{
}


Now, after we sayd what exactly we want to serialize, let's do it:
NiceClass x=new NiceClass(); // Get instance
x.SomeInt=20; // Do something
x.SomeLong=30;
x.SomeString="Bikta";
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms=new MemoryStream(); // Stream
bf.Serialize(ms,x); // "Save" object state
NiceClass y=new NiceClass();
ms.Seek(0,0); // Return stream to start
y=(NiceClass)bf.Deserialize(ms); // Restore object

Nice, is it?

But what about we want save class in SOAP format for sending via HTTP ? No problem. SoapFormatter will help us.[Serializable] property will help us as with BinaryFormatter. So:

byte[] buffer=new byte[200];
NiceClass x=new NiceClass();
x.SomeInt=20;
x.SomeLong=30;
x.SomeString="Bikta";
SoapFormatter bf = new SoapFormatter();
MemoryStream ms=new MemoryStream();
bf.Serialize(ms,x);
NiceClass y=new NiceClass();
ms.Seek(0,0);
y=(NiceClass)bf.Deserialize(ms);
ms.Seek(0,0);
ms.Read(buffer,0,200);
string s=System.Text.Encoding.ASCII.GetString(buffer,0,200);
MessageBox.Show(Form.ActiveForm,s); // Look SOAP

But what about simple XML, without SOAP-specific additions? No problem.XmlSerializer, help us!

byte[] buffer=new Byte[200];
NiceClass x=new NiceClass();
x.SomeInt=20;
x.SomeLong=30;
x.SomeString="Bikta";
XmlSerializer xs=new XmlSerializer(x.GetType());
MemoryStream ms=new MemoryStream();
xs.Serialize(ms,x);
ms.Seek(0,0);
NiceClass y;
y=(NiceClass)xs.Deserialize(ms);
ms.Seek(0,0);
ms.Read(buffer,0,200);
string s=System.Text.Encoding.ASCII.GetString(buffer,0,200);
MessageBox.Show(Form.ActiveForm,s); // XML, here are you?

But pay attention, that XmlSerializer ignores [Serializable] property! It uses it's own property named [XmlIgnore] for marking non-serialized members. So we change the class:


 public class NiceClass
{
public long SomeLong=10;
private string str="Now nothing";
[XmlIgnore] public int SomeInt=15;// Added XmlIgnore
public string SomeString
{
get
{
return str;
}
set
{
str=value;
}
}
public NiceClass()
{
}

Now it will work as we want. Pay attention, that it is no any problem to serialize private(!) string member!!! How formatters do it? What's your opinion? Thay just use "unsafe" code. But about that - in next article.. 


主站蜘蛛池模板: 午夜在线播放视频在线观看视频 | 在线成人免费观看国产精品 | 日本视频中文字幕一区二区 | 青春草国产视频 | 日本午夜小视频 | 自拍一区在线观看 | 视频午夜| 日韩久久中文字幕 | 三级三级三三级a级全黄 | 手机看片自拍自拍 | 日本在线免费观看 | 亚洲国产成人影院播放 | 伊人网在线观看 | 亚洲永久网站 | 欧美视频日韩视频 | 日本激情视频 | 亚洲第3页 | 日韩我不卡 | 日韩综合nv一区二区在线观看 | 日韩av.com| 欧美一级乱理片免费观看 | 亚洲免费观看在线视频 | 日本一区二区三区在线 视频观看免费 | 亚洲逼逼| 天堂√在线中文资源网 | 四虎最新永久免费网址 | 特级理论片 | 欧美行性性性o00x | 伊香蕉大综综综合久久 | 色噜噜狠狠狠狠色综合久不 | 亚洲成a人v在线观看 | 在线国产91| 亚洲最色 | 欧美亚洲人成网站在线观看刚交 | 青娱乐国产视频 | 亚洲成av人片在线观看无码 | 天天摸夜夜添久久精品麻豆 | 日本三级香港三级三级人!妇久 | 青青草原影视 | 五月婷婷六月色 | 亚洲精品视频免费观看 |