Object sequence

xiaoxiao2021-03-06  43

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. Using system.collections;

Public class simplegraph {

Public static void main (String [] args) {

Console.WriteLine ("Create Object Diagram"); ​​Treenode Node = New Treenode (""); FillTree (Node); Node.PrinTree (console.out);

Console.Write ("is sequenced to disk .."); stream s = (stream) file.open ("foo.xml", filemode.create, fileaccess.write; SOAPFORMATTER B = New SOAPFORMATTER () B. Serialize (s, node); s.close (); console.writeline ("Complete.");

Console.write ("is from disk-reverse sequencing object map .."); stream r = (stream) file.open ("foo.xml", filemode.open, fileaccess.read; SOAPFormatter C = new soapformatter () ;

Treenode n = (Treenode) C. DeSerialize (R); console.writeline ("Complete."); R.close (); n.printtree (console.out);

Console.Writeline ("/ r / n is exited by RETURN button."); Console.read ();}

Public static void fixtree (TREENODE Node) {type [] type = 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

Public Treenode (Object Val) {if (val == null) throw new exception ("VAL must not be empty"); 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 must not be empty"); if (Value.Equals (VAL)) Return this; Foreach (Treenode Tin Children) {Treenode W = T.Find (VAL); if (w! = null) Return W;} return null;}

Override public bool equals (object obj) {if (! (bj is turnode)) 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 = 1 ) {Sb1.append (""); sb1.append (" -"); sb2.append ("|");} Output.writeline ("{0}", SB2) Output.writeLine ("{0} {1}", sb1, value); forward (Treenode Tin Children) {T.PrintTree (Output, Offset 1);}}}

转载请注明原文地址:https://www.9cbs.com/read-52176.html

New Post(0)