问题
public class SuperCar: Car
{
public bool SuperWheels { get {return true; } }
}
public class Car
{
public bool HasSteeringWheel { get {return true;} }
}
How can I set the base class for the derived Supercar?
For example, I want to simply set SuperCars base class like this:
public void SetCar( Car car )
{
SuperCar scar = new SuperCar();
car.Base = car;
}
Basically, if I have Car objects, I do not want to manually iterate through every property of the car in order to setup the SuperCar oject, which I think is the only way you can do it but if you can do it the other way it would be sooo much better.
回答1:
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);
}
}
回答2:
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?
回答3:
This is not possible.
You need to manually set the properties.
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
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,
};
}
来源:https://stackoverflow.com/questions/4823766/derived-and-base-class-can-i-set-the-base-explicitly