【发布时间】:2019-10-03 21:43:57
【问题描述】:
我有一个 JSON 响应,它依赖于我无法直接修改的 POCO C# 对象。我需要向 POCO 对象添加一些字段,然后将它们从应用程序的任何其他重用同一对象的组件中屏蔽掉。
由于我同时控制网络服务器和客户端(但不是 POCO 对象本身),我的解决方案是从对象 T 派生,创建 List<O>,然后将其转换为 List<T>不想在派生对象O 中看到我的添加。
如果T 是O 的父级,并且简单的转换不起作用,我应该如何从一个转换为另一个?
例如
public class Parent
{
public string ParentString {get;set;}
}
public class Child : Parent
{
public string ChildTempObject {get;set;}
}
public static DoStuff()
{
List<Child> childList = new List<Child>
//... get from webservice...
return (List<Parent>)childList; // doesn't work
}
【问题讨论】:
标签: c# generics inheritance poco