【问题标题】:Serializable class inheriting from an Interface with a property of its own type可序列化类继承自具有自身类型属性的接口
【发布时间】:2023-04-10 01:44:01
【问题描述】:

我有一个接口,其中定义了与接口相同类型的属性。

public interface IMyInterface
{
    IMyInterface parent
    {
        get;
        set;
    }
}

现在,如果我声明一个类并从接口继承,我需要创建名为 parent 的属性。我希望我的类可序列化以在 Web 服务中使用,但接口在以这种方式使用时不可序列化,那么我应该如何处理 IMyInterface 类型的属性?我确实希望该属性能够序列化。

【问题讨论】:

    标签: .net serialization interface xml-serialization


    【解决方案1】:

    有趣的是:当您将接口声明替换为抽象类时,它会起作用。 .. 即使有列表。

    (好吧,其实这并不好笑……)

    public class Root
    {
        [XmlElementAttribute("ClassA", typeof(ClassA))]
        [XmlElementAttribute("ClassB", typeof(ClassB))]
        [XmlElementAttribute("ClassC", typeof(ClassC))]
        public List<IMyInterface> Items { get; set; }
    }
    
    
    public abstract class IMyInterface
    {
        IMyInterface Parent { get; set; }
        string Name { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      未测试:当您使用 XmlSerialization 时,您可以尝试为所有已知实现使用 [XmlElement] 属性来装饰您的属性。

      public interface IMyInterface
      {
          [XmlElement(Type=typeof(App.Projekt), ElementName="Projekt")]
          [XmlElement(Type=typeof(App.Person), ElementName="Person")]
          [XmlElement(Type=typeof(App.Task), ElementName="Task")]
          IMyInterface parent
          {
              get;
              set;
          }
      }
      

      未测试 - 我不知道这是否也适用于接口。

      编辑:我用这段代码测试了这个问题。它没有用。我认为,XmlElement 将与“对象”类型的属性执行相同的操作。

      public interface IMyInterface
      {
          IMyInterface Parent { get; set; }
          string Name { get; set; }
      }
      
      public class ClassA : IMyInterface
      {
          [XmlElement(Type = typeof(ClassA), ElementName = "ClassA")]
          [XmlElement(Type = typeof(ClassB), ElementName = "ClassB")]
          [XmlElement(Type = typeof(ClassC), ElementName = "ClassC")]
          public IMyInterface Parent { get; set; }
          public string Name { get; set; }
      
          public string AProperty { get; set; }
      }
      
      public class ClassB : IMyInterface
      {
          [XmlElement(Type = typeof(ClassA), ElementName = "ClassA")]
          [XmlElement(Type = typeof(ClassB), ElementName = "ClassB")]
          [XmlElement(Type = typeof(ClassC), ElementName = "ClassC")]
          public IMyInterface Parent { get; set; }
          public string Name { get; set; }
      
          public string BProperty { get; set; }
      }
      
      public class ClassC : IMyInterface
      {
          [XmlElement(Type = typeof(ClassA), ElementName = "ClassA")]
          [XmlElement(Type = typeof(ClassB), ElementName = "ClassB")]
          [XmlElement(Type = typeof(ClassC), ElementName = "ClassC")]
          public IMyInterface Parent { get; set; }
          public string Name { get; set; }
      
          public string CProperty { get; set; }
      }
      

      例外是:

      "无法序列化成员 TestXMLSerializer.ClassA.Parent of 类型 TestXMLSerializer.IMyInterface 因为它是一个接口。”

      【讨论】:

      • 似乎不起作用。我也尝试使用 XmlInclude 属性也没有运气
      猜你喜欢
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多