cast the Parent object to Child object in C#

前端 未结 7 1243
春和景丽
春和景丽 2020-12-01 07:00

Hi i want to cast the Parent object to Child object in C#

public class Parent
{
    public string FirstName {get; set;}
    public string LastName {get; set;         


        
7条回答
  •  醉梦人生
    2020-12-01 07:14

    var lstChild = lstParent.Cast().ToList();
    

    or

    var lstChild = lstParent.ConvertAll(x=>(Child)x);
    

    Both of these, however, assume that the Parent list actually contains Child instances. You can't change the actual type of an object.

提交回复
热议问题