Life, it seems, will fade away

          drifting further every day

对象序列化

using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Collections;

public class SimpleGraph {


    public static void Main(String[] args) {

        Console.WriteLine ("创建对象图");
        TreeNode node = new TreeNode ("");
        FillTree (node);
        node.PrintTree (Console.Out);

        Console.Write ("正在将对象图序列化到磁盘..");
        Stream s = (Stream)File.Open("foo.xml", FileMode.Create, FileAccess.Write);
        SoapFormatter b = new SoapFormatter ();
        b.Serialize(s, node);
        s.Close();
        Console.WriteLine ("完成。");

        Console.Write ("正在从磁盘反序列化对象图..");
        Stream r = (Stream)File.Open("foo.xml", FileMode.Open, FileAccess.Read);
        SoapFormatter c = new SoapFormatter ();

        TreeNode n = (TreeNode) c.Deserialize(r);
        Console.WriteLine ("完成。");
        r.Close();
        n.PrintTree (Console.Out);

        Console.WriteLine ("/r/n按 Return 键退出。");
        Console.Read();
    }

    public static void FillTree (TreeNode node) {
        Type [] types = typeof (object).Module.Assembly.GetTypes();
        node.AddChild (new TreeNode (typeof(object).FullName));


        foreach (Type t in types) {
            if (t.BaseType == null) continue;
            if (!t.IsPublic) continue;
            TreeNode n = node.Find (t.BaseType.FullName);
            if (n != null) n.AddChild (new TreeNode (t));
        }
    }
}

[Serializable] public class TreeNode : ISerializable {
    private Object value;

    private ArrayList children;


    private TreeNode (SerializationInfo info, StreamingContext c) {
        value = info.GetValue ("value", typeof(object));
        children = new ArrayList ();
        Object o;

        for (int i = 1; i < info.MemberCount; i++) {
            o = info.GetValue (i.ToString(), typeof(object));
            children.Add (o);
        }
    }

    public TreeNode (Object val) {
        if (val == null) throw new Exception ("val 一定不能为空");
        value = val;
        children = new ArrayList ();
    }

    void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) {
        if (value == null) info.AddValue ("value", "NULL");
        else info.AddValue ("value", value.ToString() + "(SAVED)");
        int i = 1;
        foreach (object o in children) {
            if (o == null) continue;
            info.AddValue (i.ToString(), o);
            i++;
        }
    }

    public Object Value {
        get {
            return value;
        }
    }


    public void AddChild (TreeNode child) {
        if (!children.Contains (child))
            children.Add (child);
    }

    public TreeNode Find (Object val) {
        if (val == null) throw new Exception ("val 一定不能为空");
        if (value.Equals (val)) return this;
        foreach (TreeNode t in children) {
            TreeNode w = t.Find (val);
            if (w != null) return w;
        }
        return null;
    }

    override public bool Equals (Object obj) {
        if (! (obj is TreeNode)) return false;
        TreeNode t = (TreeNode) obj;
        return (t.value.Equals (this.value));
    }

    override public string ToString () {
        return value.ToString ();
    }

    public void PrintTree (TextWriter  output) {
        PrintTree (output, 0);
    }

    private void PrintTree (TextWriter  output, int offset) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        for (int i =0; i < offset-1; i++) {
            sb1.Append (" ");
            sb1.Append ("|");
            sb2.Append (" ");
            sb2.Append ("|");
        }
        if (offset >=1) {
            sb1.Append (" ");
            sb1.Append ("+-");
            sb2.Append (" ");
            sb2.Append ("|");
        }
        output.WriteLine ("{0}", sb2);
        output.WriteLine ("{0}{1}", sb1, value);
        foreach (TreeNode t in children) {
            t.PrintTree (output, offset+1);
        }
    }
}

阅读更多
个人分类: 我的收藏
想对作者说点什么? 我来说一句

java对象序列化.ppt

2011年04月23日 44KB 下载

Java对象序列化详细解析.doc

2009年03月04日 253KB 下载

对象序列化TCP传输

2011年04月09日 83KB 下载

c#对象序列化与反序列化实例

2009年11月24日 57KB 下载

.NET对象序列化与数据持久化

2011年06月23日 46KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭