现在我的需求是,将一个类实例(里面存放了一些数据)强转换成字符串,然后发送出去,那边接收后再转成类实例,取其中的数据。
好象有个object装箱,我这么写编译通过,运行出错。
一边:
classA a = new classA();
…………………………
object b = a;
string c = (string)b;//这里出问题。
发送另一边接受到string c;
object b = c;
classA a = (classA)b;
…………………………我应该怎么写?谢谢帮忙。
另外再问一下,转成字符串后,我再在后面加几个字符用来判断是哪种类型。
接收后,去掉那几个字符再转成需要的类型,应该没影响吧?
好象有个object装箱,我这么写编译通过,运行出错。
一边:
classA a = new classA();
…………………………
object b = a;
string c = (string)b;//这里出问题。
发送另一边接受到string c;
object b = c;
classA a = (classA)b;
…………………………我应该怎么写?谢谢帮忙。
另外再问一下,转成字符串后,我再在后面加几个字符用来判断是哪种类型。
接收后,去掉那几个字符再转成需要的类型,应该没影响吧?
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);
}
}
}