Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to overwrite the inherited property values of derived class with that of base class instance
    primarykey
    data
    text
    <p>I have a <strong>Human</strong> class with simple two properties <em>Age</em> and <em>Name</em>. Then, I have another class <strong>Woman</strong> derived from the <strong>Human</strong> class and it has one extra properties <em>FavCream</em>. Now, I have two different instances of these classes say <strong>h1</strong>(of base class <strong>Human</strong>) and <strong>w1</strong> (of derived class <strong>Woman</strong>) and I simply want to overwrite all the derived properties of <strong>w1</strong> with the corresponding values from <strong>h1</strong>. </p> <p>However, I did not found this built-in. I do not like to create a public method on Woman class which manually does the assignment job. I think, I can create a utility function using reflection feature but looking for proven way to deal with this.</p> <p>Is this impossible? Can not we have any advantage of inheritance in this scenario? or Is this illogical? </p> <p>Please have patience with me. Thanks!</p> <pre><code>class Program { private static Woman _aWoman; static void Main(string[] args) { var aHuman = new Human() { Age = 25, Name = "Ram" }; Program._aWoman = new Woman() { Age = 22, FavCream = "Ponds" , Name = "Sita"}; Console.WriteLine("Woman is " + Program._aWoman.ToString()); // prints Age = 22 Name = Sita FavCream = Ponds Console.WriteLine("Human is " + aHuman.ToString()); // prints Age = 25 Name = Ram **// Do something here so woman derived property has auto over written with property of human instance.** Console.WriteLine("after overwriting, woman is " + _aWoman.ToString()); // The result I m looking for here is "Age = 25 Name = Ram FavCream = Ponds" } } internal class Human { public int Age { get; set; } public string Name { get; set; } public override string ToString() { return "Age = " + this.Age + " Name = " + this.Name; } } internal class Woman: Human { public string FavCream { get; set; } public override string ToString() { return base.ToString() + " FavCream = " + this.FavCream; } } </code></pre> <p><strong>UPDATE:</strong> I ended on creating a method and copied the base class properties using reflection. Also, I tried to enhance this with generic restriction but not succeeded yet. </p> <p>Thanks everyone for such quick reply.</p> <pre><code>// Trying to make it generic... but could not enforce contraint that T must be base class of 'this' public void Overwrite&lt;T&gt;(T baseInstance) where T : Human { Type baseType = baseInstance.GetType(); Type derivedType = this.GetType(); PropertyInfo[] propInfos = null; propInfos = baseType.GetProperties(); foreach (PropertyInfo eachProp in propInfos) { PropertyInfo baseProp = (baseType).GetProperty(eachProp.Name); object baseVal = baseProp.GetValue(baseInstance, null); eachProp.SetValue(this, baseVal, null); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload