C# Casting a List as List

前端 未结 11 1570
小鲜肉
小鲜肉 2020-12-09 15:35

Why can I not cast a List as List? Why does the following not work:

internal class ObjBase
   {
   }

int         


        
11条回答
  •  天涯浪人
    2020-12-09 16:11

    Linq has a ConvertAll method. so something like

    list.ConvertAll(objBase => objbase.ConvertTo(obj));
    

    I'm not sure what else to suggest. I assume ObjBase is the base class, and if all ObjBase objects are Obj objects, i'm not sure why you would have the two objects in the first place. Perhaps i'm off the mark.

    Edit: the list.Cast method would work better than the above, assuming they are castable to each other. Forgot about that until I read the other answers.

提交回复
热议问题