在程序中定义一个基类,三个子类都继承这个基类,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;
            }
        }

解决方案 »

  1.   

          public void test()
          {          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;
              }
          }
      }
    }
      

  2.   

    读xml根节点名称,反射获取类型。string rootNodeName="A";
    Type type = Assembly.GetExecutingAssembly().GetType("命名空间."+rootNodeName);
    XmlSerializer xs = new XmlSerializer(type);
      

  3.   

    多谢回复,可能我还没有说清楚,下面代码你是知道是A类的情况下传的,我的意思是如果不知道是哪个具体子类呢?要根据actionType 判断
    XmlBase mb = (XmlBase)LoadXml(filePath, typeof(A));
      

  4.   

    你没看到我把他转化成的是基类么。然后我再根据他的actiontype属性的值来判断是A,B还是C
      

  5.   

    但是我要用XmlBase mb = (XmlBase)LoadXml(filePath, typeof(XmlBase));报错误XML 文档(2, 2)中有错误。
      

  6.   

    sorry,我没看清楚
     XmlBase mb = (XmlBase)LoadXml(filePath, a.GetType());//调用实例的GetType()方法即可
      

  7.   

    然后下边的
     switch (mb.actionType)
      {  
      case "a":
      Console.WriteLine("The Tyep is A");
      break;  }
    这个地方也不用了
    直接用mb.GetType();就行了
      

  8.   

    XmlBase mb = (XmlBase)LoadXml(filePath, a.GetType());不过还是知道是A类型了啊,我是2个系统之间传递数据的,另外系统不可能知道类型a.GetType()
      

  9.   

    首先你的类XmlBase、A、B、C应该在一个独立的程序集中,作为公共的程序集。
    你的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
      

  10.   

    不知道类型也没有关系,直接传递对象就行了啊。。然后在另一个系统来获得这个对象的类型。。
    假如XX x = new XX();
    然后你可把这个x作为一个参数来传递,然后在需要类型的时候使用x.GetType();不就行了吗