Derived and base class, can I set the base explicitly?

流过昼夜 提交于 2019-12-04 22:53:26

I use something like this in the subclass and it works fine for me:

using System.Reflection;
.
.
.
/// <summary> copy base class instance's property values to this object. </summary>
private void InitInhertedProperties (object baseClassInstance)
{
    foreach (PropertyInfo propertyInfo in baseClassInstance.GetType().GetProperties())
    {
        object value = propertyInfo.GetValue(baseClassInstance, null);
        if (null != value) propertyInfo.SetValue(this, value, null);
    }
}

If I'm understanding your question correctly (and I'm not entirely sure that I am), then you can sort of get the behavior you want by doing something like this:

class Car {
    public bool CarProperty { get; set; }
    // regular constructor
    public Car() {

    }
    // "copy" constructor
    public Car(Car c) {
        CarProperty = c.CarProperty;
    }
}

class SuperCar : Car {
    public bool SuperCarProperty { get; set; }
    // regular constructor
    public SuperCar() {
    }
    // "copy" constructor
    public SuperCar(Car c) : base(c) {
        SuperCar sc = c as SuperCar;
        if(sc != null) {
            SuperCarProperty = sc.SuperCarProperty;
        }
    }

Then you can do this:

public void SetCar(Car car) {
    SuperCar scar = new SuperCar(car);
}

Note that you have to be very careful in your "copy" constructor not to copy properties in such a way that two objects share the same members (references) when they should not.

I have to ask, though, what your goal is with this?

This is not possible.

You need to manually set the properties.

Overall a lot of helpful comments. I think the short answer was given by pst and I think this is correct:

No. Short reason: There is no separate base object. (object)this == (object)base is always true. There are ways to perform cloning/copying by reflection (and other means) though. Perhaps describe what is really wanted

So, his suggestion of using the automapper tool was also incredibly useful and was basically what I was looking for.

You can only copy the contents of another class, but not set it directly. See example below using what is called a copy constructor.

class Car
{
    string model;
    public Car(string model) { this.model = model; }
    protected Car(Car other) { this.model = other.model; }
    string Model { get; set; }
}

class SuperCar : Car
{
    public SuperCar(Car car) : base(car) { }
    public SuperCar(string model) : base(model) { }
    bool IsTurbo { get; set; }
}

The key is the keyword base() after the constructor declaration.

First of all, Each SuperCar object is different from the other. Inheritance helps you define common behaviour of related objects, It does not mean they are the same objects. Each object of a type has its own construction process. Before a child object is created, the object of base class is created as per the inheritance hierarchy. If you wish to use a common property for all SuperCar objects, you'll need to maintain a seperate instance of the Car object within SuperCar instance, assign the common car object into the car object in SuperCar and override the properties derived from the base Car object and redirect it to props. of internal Car object.

public class SuperCar: Car
{
     public bool SuperWheels { get {return true; } }

     Car carInstance = null;
     public void SetCar( Car car )
     {
         carInstance = car; 
     }
}

But I stiill believe, there's something wrong with this design the way you want to do it. If you can let us know the larger picture as to why you want to do this, we may come up with better design to address your requirement.

If you would like to have more control than copy reflected properties try to apply Builder pattern. T can be any class derived from BaseClass.

 public static T BuildNew<T>(BaseClass baseClass) where T : BaseClass, new()
 {
    return new T
    {
        Property1 = baseModel.Property1,
        Property2 = baseModel.Property2,
    };
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!