使用泛型类型进行父子类转换
本文关键字:父子 子类 转换 泛型类型 | 更新日期: 2023-09-27 17:51:14
我有三个类命名为OptionFeature
, Fabric
, Material
Fabric
是Material
的子类。
OptionFeature<Fabric>
的对象。如何将此对象转换为OptionFeature<Material>
试试这样
class Program
{
static void Main(string[] args)
{
OptionFeature optionFeature = new OptionFeature();
optionFeature.material = new Material();
Fabric fabric = (Fabric)optionFeature.material;
List<Material> materials = new List<Material>();
List<Fabric> fabrics = materials.Select(x => (Fabric)x).ToList();
}
}
public class OptionFeature
{
public Material material {get;set;}
}
public class Material
{
}
public class Fabric : Material
{
}