对象序列化

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值