构造函数中的基对象作为向下转型的替代方案
我有一个基础对象 (RTUDevice) 列表,想要迭代每个对象并将其转换为派生对象(实际上是派生 RTDSensor 的派生对象),但是向下转换会引发错误。
public RTUDevice(int id)
{
_id = id;
}
public class RTDDevice : RTUDevice
{
public RTDDevice(int id)
: base(id)
{
}
}
public class RTDSensor : RTDDevice
{
public RTDSensor(int id)
: base(id)
{
}
}
RTDSensor return = (RTDSensor)_devices.Find(d => d.Id == p.ReturnId);
将构造函数中的基础对象传递给 RTDSensor 会更好吗?
public RTDSensor(RTUDevice rtu) : base(rtu.Id)
{
}
或者我的 OOP 设计偏离了目标。
I have a list of base objects (RTUDevice) and want to iterate through and convert each to a derived object (actually a derived of a derived RTDSensor) , however the downcasting is throwing an error.
public RTUDevice(int id)
{
_id = id;
}
public class RTDDevice : RTUDevice
{
public RTDDevice(int id)
: base(id)
{
}
}
public class RTDSensor : RTDDevice
{
public RTDSensor(int id)
: base(id)
{
}
}
RTDSensor return = (RTDSensor)_devices.Find(d => d.Id == p.ReturnId);
Would it be better to pass the base object in a constructor to RTDSensor like
public RTDSensor(RTUDevice rtu) : base(rtu.Id)
{
}
or is my OOP design way off the mark.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能在于您将设备添加到 _devices 集合的方式。
例如:
应该有效。 但是,如果将传感器添加为 RTUDevice 对象,则之后无法将其转换为 RTDSensor,因为它没有 RTDSensor 携带的附加信息。
必须使用。
参考 Jon Skeet 的示例,如果您稍后想要将 o 转换为 FileStream 对象,则
The problem could be with the way you're adding the devices to the _devices collection.
For example:
should work. However if you add the sensor as an RTUDevice object you can't cast it to RTDSensor afterwards because it doesn't have the additional information that RTDSensor carries.
Referring to Jon Skeet's example you have to use
If you later want to cast o to a FileStream object.
我不认为这真的与构造函数调用有任何关系 - 问题是你试图将一种类型的对象转换为另一种类型。 这永远不会调用构造函数,除非您涉及用户定义的转换(如果涉及的两种类型位于同一继承层次结构中,则不能。)
如果您想创建一个new更多派生类型的对象,请继续:
如果您想转换所有对象,并且这是一个
List
,那么您可以使用:或者更多LINQ基于 -的方法:
...但是您不能只是告诉 .NET 将对象视为比实际类型更具体的类型。 为了让它达到荒谬的长度,您希望它做什么:
编辑:我假设您无法更改
_devices
集合中的内容。 如果您可以确保它包含适当类型的对象,那就太好了 - 如果没有,您稍后需要创建新对象。I don't think this really has anything to do with the constructor call - the problem is you're trying to cast an object of one type to another. That never calls a constructor, unless you've got a user-defined conversion involved (which you can't if the two types involved are in the same inheritance hierarchy.)
If you want to create a new object of the more derived type, go ahead:
If you want to convert all the objects, and this is a
List<RTUDevice>
then you could use:or the more LINQ-based approach:
... but you can't just tell .NET to treat an object as if it were a more specific type than it actually is. To take it to a ridiculous length, what would you expect this to do:
EDIT: I've assumed that you can't change what's in the
_devices
collection. If you can make sure that it contains objects of the appropriate type to start with, that's great - if not, you'll need to create the new objects later.