c# - 使用接口(interface)序列化子集合

标签 c# serialization interface

我有这样的界面

public interface IA{
    IEnumerable<IB> Items {get;set;}
}

public interface IB{
    int ID {get;set;}
}

如果我尝试序列化接口(interface),我会得到一个错误

Cannot serialize interface

如果我创建了界面的具体版本,则无法创建集合的具体版本。

public class classA:IA{
   public IEnumerable<B> Items {get;set;}  //this doesn't match the interface
}

但如果我执行以下操作,我们仍然会遇到序列化问题

public class classA:IA{
   public IEnumerable<IB> Items {get;set;}
}

最佳答案

您确定要界面吗?泛型可能会帮助您序列化不同类型的 IEnumerable。

关于c# - 使用接口(interface)序列化子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5639212/

相关文章:

c# - 通用类型层次结构的 DataContractSerialization

c# - WCF 类型问题?

java - 多态对象的序列化

Java 反射 - 获取通用接口(interface)中 T 的实际类型<T>

PHP接口(interface)问题-找不到类

c# - 看似没有错误的非工作 C# 代码

c# - 无论客户端发生什么情况,如何使服务器程序继续运行?

c# - Entity Framework 导航属性外键上的列名称无效

java - 如何在JAVA中将附件对象转换为ByteArray

Java8 : Why is it forbidden to define a default method for a method from java. lang.Object