cast the Parent object to Child object in C#

前端 未结 7 1241
春和景丽
春和景丽 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:04

    Another way to approach this solution is to have the Parent control the copy logic and Child will pass the copy source into the base constructor.

    e.g. iterating on Avner's solution

    public class Parent
    {
        public string FirstName {get; set;}
        public string LastName {get; set;}
        public string City {get; set;}
    
        public Parent()
        {
        }
    
        public Parent(Parent copyFrom)
        {
            this.FirstName = copyFrom.FirstName;
            this.LastName = copyFrom.LastName;
            this.City = copyFrom.City;
        }
    }
    
    public class Child : Parent
    {
        public string PhoneNumber {get; set;}
        public string MobileNumber {get; set;}
    
        public Child (Parent parentToCopy) : base(parentToCopy)
        {
        } 
    }
    
    0 讨论(0)
  • 2020-12-01 07:06

    I did like this:

    class Parent
    {
      ...
    }
    
    class Child :Parent
    {
      ...
      public Child(Parent p)
      {
                foreach (FieldInfo prop in  p.GetType().GetFields())
                    GetType().GetField(prop.Name).SetValue(this, prop.GetValue( p));
    
                foreach (PropertyInfo prop in  p.GetType().GetProperties())
                    GetType().GetProperty(prop.Name).SetValue(this, prop.GetValue( p, null), null);
      }
    }
    
    0 讨论(0)
  • 2020-12-01 07:14
    var lstChild = lstParent.Cast<Child>().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.

    0 讨论(0)
  • 2020-12-01 07:15

    I do so (this is just an example):

    using System.Reflection;
    
    public class DefaultObject
    {
        ...
    }
    
    public class ExtendedObject : DefaultObject
    {
        ....
        public DefaultObject Parent { get; set; }
    
        public ExtendedObject() {}
        public ExtendedObject(DefaultObject parent)
        {
            Parent = parent;
    
            foreach (PropertyInfo prop in parent.GetType().GetProperties())
                GetType().GetProperty(prop.Name).SetValue(this, prop.GetValue(parent, null), null);
        }
    }
    

    Using:

    DefaultObject default = new DefaultObject { /* propery initialization */ };
    ExtendedObject extended = new ExtendedObject(default); // now all properties of extended are initialized by values of default properties.
    MessageBox.Show(extended.Parent.ToString()); // now you can get reference to parent object
    
    0 讨论(0)
  • 2020-12-01 07:24

    You may use reflection as well, but this is simpler for your case.

    foreach(var obj in lstParent)
    {
        Child child = new Child(){ FirstName = obj.FirstName, LastName=obj.LastName, City = obj.City};
        child.MobileNumber = "some mobile number";
        child.PhoneNumber = "some phone number";
        lstChild.Add((Child)obj);
    }
    
    0 讨论(0)
  • 2020-12-01 07:30

    This is what I came up with form my solution.

        public static void ShallowConvert<T, U>(this T parent, U child)
        {
            foreach (PropertyInfo property in parent.GetType().GetProperties())
            {
                if (property.CanWrite)
                {
                    property.SetValue(child, property.GetValue(parent, null), null);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题