简体   繁体   中英

parent child class conversion with generic type

I have 3 classes named OptionFeature , Fabric , Material

Fabric is a child class of Material .

Now i have an object of type OptionFeature<Fabric> . How to convert this object into OptionFeature<Material>

Try something like this

    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
    {
    }​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM