·首页 ·asp ·.net ·php ·JSP ·CGI ·数据库 ·网页设计 ·网管专栏 ·XML ·工具软件 ·办公软件 ·操作系统 ·程序设计 ·LINUX 
  当前位置: 普克>>计算机教程>>.net>>Windows开发>>关于序列化-开心,转来一篇
flash视频教学

photoshop专题

asp.net专题

office专题

关于序列化-开心,转来一篇


Windows开发 发表时间:2006-4-8 字体:  返回
建立一个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.. 


上一篇:关于序列化--这篇完整些,但是是for beta1的
下一篇:如何控制窗口的关闭事件

普克创业投资网刊载此文不代表同意其说法或描述,仅为提供更多信息。
在百度中搜索关于序列化-开心,转来一篇的相关内容]   [在狗狗中搜索关于序列化-开心,转来一篇的相关内容]
Copyright @ 2006 PUPK.COM 普克创业投资网 版权所有
 建议使用1024*768以达到最好的浏览效果