在程序中定义一个基类,三个子类都继承这个基类,XML 反序列化时候不知道具体类型是A,B还是C。
怎么实现反序列化,然后根据actionType去判断是A还是B,C?????
1)我用typeof(object))反序列化,一直报错误。大家帮忙看看什么问题呢?
2)我看有人说不用基类,用Object来反序列化,然后使用GetType()得到类型信息。我用Object序列化A的时候怎么就报错呢?大家能给出正确的代码吗,实在是非常急!!!!! abstract public class XmlBase
{
public string actionType; public XmlBase()
{
}
}
public class A: XmlBase
{
public A()
{ }
}
public class B: XmlBase
{
public B()
{ }
}
public class C: XmlBase
{
public C()
{ }
}
public class MyClass
{
public void test()
{
string filePath = "";
A a = new A();
a.actionType = "a";
SaveXml(filePath,a);
LoadXml(filePath, typeof(A));
//如果loadxml的时候不知道具体类型是A,B还是C。
//怎么实现load,然后根据actionType去判断是A还是B,C?????
} // OBJECT -> XML
public static void SaveXml(string filePath, object obj)
{ SaveXml(filePath, obj, obj.GetType()); }
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
// XML -> OBJECT
public static object LoadXml(string filePath, System.Type type)
{
if (!System.IO.File.Exists(filePath))
return null;
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
}
怎么实现反序列化,然后根据actionType去判断是A还是B,C?????
1)我用typeof(object))反序列化,一直报错误。大家帮忙看看什么问题呢?
2)我看有人说不用基类,用Object来反序列化,然后使用GetType()得到类型信息。我用Object序列化A的时候怎么就报错呢?大家能给出正确的代码吗,实在是非常急!!!!! abstract public class XmlBase
{
public string actionType; public XmlBase()
{
}
}
public class A: XmlBase
{
public A()
{ }
}
public class B: XmlBase
{
public B()
{ }
}
public class C: XmlBase
{
public C()
{ }
}
public class MyClass
{
public void test()
{
string filePath = "";
A a = new A();
a.actionType = "a";
SaveXml(filePath,a);
LoadXml(filePath, typeof(A));
//如果loadxml的时候不知道具体类型是A,B还是C。
//怎么实现load,然后根据actionType去判断是A还是B,C?????
} // OBJECT -> XML
public static void SaveXml(string filePath, object obj)
{ SaveXml(filePath, obj, obj.GetType()); }
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
// XML -> OBJECT
public static object LoadXml(string filePath, System.Type type)
{
if (!System.IO.File.Exists(filePath))
return null;
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
}
解决方案 »
- 如何用C# 取网页AJAX返回的数据?
- WinForm程序 脱离.NET框架的问题????
- C#传递结构体给C++写的Dll,结构体里面嵌套了结构体指针, 取到了值,结构体指针的值没能取得到?
- string to DateTime的问题
- Winform 打印
- 100分求关于treeview的问题~!点击node响应事件。
- C# 自定义控件 监听 主窗体事件 load
- 高手请教:字符串加密和解密的问题
- 如何设置textbox中插入字符的位置?等待...
- 【付费求助】5000元求大神写一个 appstore账号全自动注册器
- 请求啊! 正规则问题!!!
- 关于DATAGRIDVIEW与ILIST<OBJECT>绑定的问题
{ string filePath = @"e:\test.xml";
A a = new A();
a.actionType = "a";
SaveXml(filePath, a);
XmlBase mb = (XmlBase)LoadXml(filePath, typeof(A));//用基类接收,判断他的actiontype
//如果loadxml的时候不知道具体类型是A,B还是C。
//怎么实现load,然后根据actionType去判断是A还是B,C?????
switch (mb.actionType)
{
case "a":
Console.WriteLine("The Tyep is A");
break; }
}
public static void Main(string[] args)
{
MyClass m = new MyClass();
m.test();
Console.ReadKey();
} // OBJECT -> XML
public static void SaveXml(string filePath, object obj)
{ SaveXml(filePath, obj, obj.GetType()); }
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
// XML -> OBJECT
public static object LoadXml(string filePath, System.Type type)
{
if (!System.IO.File.Exists(filePath))
return null;
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
}
}
}
Type type = Assembly.GetExecutingAssembly().GetType("命名空间."+rootNodeName);
XmlSerializer xs = new XmlSerializer(type);
XmlBase mb = (XmlBase)LoadXml(filePath, typeof(A));
XmlBase mb = (XmlBase)LoadXml(filePath, a.GetType());//调用实例的GetType()方法即可
switch (mb.actionType)
{
case "a":
Console.WriteLine("The Tyep is A");
break; }
这个地方也不用了
直接用mb.GetType();就行了
你的2个项目都引用这个程序集。
先读取xml文件的根结点名称,string rootName="A";
Type type=null;
swictch(rootName)
{
case "A":type=typeof(A);
case "B":type=typeof(A);
case "C":type=typeof(A);
}
以后就可以反序列化了。如果不想switch判断,可以在公共程序集中使用工厂方法,反射方式返回type
假如XX x = new XX();
然后你可把这个x作为一个参数来传递,然后在需要类型的时候使用x.GetType();不就行了吗