Cannot convert from List to List

前端 未结 6 1322
庸人自扰
庸人自扰 2020-11-22 10:39

I\'m trying to pass A list of DerivedClass to a function that takes a list of BaseClass, but I get the error:

cannot convert from 
         


        
6条回答
  •  遇见更好的自我
    2020-11-22 11:30

    A solution i used was to create an extension class:

    public static class myExtensionClass 
    {
        public void doSomething(List bc) where T: BaseClass
        {
            // do something with bc
        }
    }
    

    It uses generic, but when you call it you don't have to especify the class since you already 'told' the compiler the type is the same of extended class.

    You would call it this way:

    List lst = new List();
    lst.doSomething();
    

提交回复
热议问题