Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast the Parent object to Child object in C#

Tags:

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;}     public string City {get; set;} }  public class Child : Parent {     public string PhoneNumber {get; set;}     public string MobileNumber {get; set;} } 

now the scenario is is a list of parent object and i want to generate list of child object so that i can have extended information

List<Parent> lstParent; List<Child> lstChild = new List<Child>();  foreach(var obj in lstParent) {     lstChild.add((Child)obj); } 

as child class inherited parent class so the child class already have the parent class member i just want to fill them automatically so that i can populate datamember of child class

like image 901
Adnan Zameer Avatar asked Mar 27 '12 08:03

Adnan Zameer


People also ask

Can we assign parent object to child objects?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.

Can we typecast parent to child in C#?

Parent to Child (Explicit casting - Can be successful) Note: Because objects has polymorphic nature, it is possible for a variable of a parent class type to hold a child type. Conclusion : After reading above all, hope it will make sense now like how parent to child conversion is possible(Case 3).

Can children reference parent object class?

In Java, the reference variable of the Parent class is capable to hold its object reference as well as its child object reference.

Can you cast a parent object to child Java?

However, we can forcefully cast a parent to a child which is known as downcasting. After we define this type of casting explicitly, the compiler checks in the background if this type of casting is possible or not. If it's not possible, the compiler throws a ClassCastException.


2 Answers

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 
like image 104
ALT Avatar answered Oct 06 '22 00:10

ALT


If I understand your "I just want to fill them automatically" comment correctly, you want to create a new Child object that's populated with the values of the Parent, with default values for the new properties. Best way to do that is to create a constructor that copies the values:

public class Parent {    public string FirstName {get; set;}     public string LastName {get; set;}     public string City {get; set;} }  public class Child : Parent {     public string PhoneNumber {get; set;}     public string MobileNumber {get; set;}      public Child (Parent parentToCopy)     {         this.FirstName = parentToCopy.FirstName;         this.LastName = parentToCopy.LastName;         this.City = parentToCopy.City;          this.PhoneNumber = string.Empty; // Or any other default.         this.MobileNumber = string.Empty;     }  } 

Now you can use LINQ, like the answers above, to create a Child out of each Parent:

List<Child> lstChild = lstParent.Select(parent => new Child(parent)).ToList(); 

Note that this is very similar to @daryal's answer, but wraps the parent-to-child copying logic inside the constructor, rather than having it outside in the new Child() call.

like image 23
Avner Shahar-Kashtan Avatar answered Oct 06 '22 01:10

Avner Shahar-Kashtan