派生基类,可以显式设置基类吗? [英] Derived and base class, can I set the base explicitly?

查看:81
本文介绍了派生基类,可以显式设置基类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

例如,我只想像这样设置SuperCars基类:

For example, I want to simply set SuperCars base class like this:

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

基本上,如果我有Car对象,我不想手动遍历为了设置SuperCar对象,必须设置汽车的所有属性,我认为这是您可以执行此操作的唯一方法,但是如果您可以以其他方式执行此操作,效果会更好。

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.

推荐答案

总体上有很多有用的评论。我认为简短的答案是由pst给出的,我认为这是正确的:

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

不。简短原因:没有单独的基础对象。 (对象)此==(对象)基础始终为true。尽管有一些方法可以通过反射(和其他方式)执行克隆/复制。也许描述一下真正想要的东西

因此,他关于使用自动映射器工具的建议也非常有用,并且基本上就是我想要的。

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

这篇关于派生基类,可以显式设置基类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆