简体   繁体   English

转换继承的对象

[英]Casting an inherited object

I guess that this is a simple question for most of you but none of the answers I have seen on the web are what I need. 我想对大多数人来说这是一个简单的问题,但我在网上看到的答案都不是我需要的。 So, I have a base class and another one that inherits it: 所以,我有一个基类和另一个继承它的基类:

public class MyBase
{
    public DateAdded { get; set; }
}

public class MySecond : MyBase
{
    public MySecond(MyBase prototype) 
    {
        this = (MySecond)prototype; // this is read-only and does not work
    }

    public DateDeleted { get; set; }
}

How to instanciate a MyBase, provide it as a prototype to another instance of MySecond and make sure the latter gets all the values of the prototype without explicitly transferring the values one-by-one? 如何实现MyBase,将其作为原型提供给另一个MySecond实例,并确保后者获取原型的所有值,而不是逐个显式地传递值?

You can't, basically. 基本上你不能。 You could use reflection to copy the values in an automated way, but it would still be copying them one-by-one, and wouldn't give you as much control over how they were copied (eg whether you needed to make a deep copy). 您可以使用反射以自动方式复制值,但它仍然会逐个复制它们,并且不会对您如何复制它们提供尽可能多的控制(例如,您是否需要进行深层复制)。

As dasblinkenlight states, it may be more appropriate to make each class responsible for copying its own properties, unless that's not something that is always appropriate. 正如dasblinkenlight所述,让每个类负责复制自己的属性可能更合适,除非这不是总是合适的。

Note that you might want to check whether prototype is itself an instance of MySecond , and copy the properties from that if so. 请注意,您可能想要检查prototype 本身是否是MySecond的实例,如果是,则从中复制属性。

Also note that if you use composition instead of inheritance, and try to make classes immutable, a lot of the time this kind of thing becomes unnecessary or trivial. 还要注意,如果你使用组合而不是继承,并尝试使类不可变,很多时候这种事情变得不必要或微不足道。 It's not always appropriate, but I see quite a lot of overuse of inheritance. 它并不总是合适的,但我看到很多过度使用继承。

You cannot do the copying automatically, but you can do it in a shared spot - ie in the constructor of the MyBase class. 您无法自动执行复制,但您可以在共享位置执行复制 - 即在MyBase类的构造函数中。 This way all derived classes would be able to use the same shared code if you pass the prototype as the parameter of your base constructor, like this: 这样,如果将prototype作为基础构造函数的参数传递,则所有派生类都可以使用相同的共享代码,如下所示:

public class MyBase {
    public DateAdded { get; set; }
    public MyBase() {
    }
    public MyBase(MyBase other) {
        // Do the copying here
    }
}

public class MySecond : MyBase {
    public MySecond(MyBase prototype) : base(prototype) {
    }

    public DateDeleted { get; set; }
}

EDIT (in response to a comment) 编辑 (回应评论)

If you do not have access to the source of MyBase , you can make a static helper method that does the copying for you, like this: 如果您无法访问MyBase的源MyBase ,则可以创建一个静态帮助程序方法,为您执行复制,如下所示:

internal static class MyBaseHelper {
    public static void CopyFromBase(this MyBase target, MyBase source) {
        target.DateAdded = source.DateAdded;
    }
}

Now you can use it in your derived classes like this: 现在,您可以在派生类中使用它,如下所示:

public class MySecond : MyBase {
    public MySecond(MyBase prototype) {
        this.CopyFromBase(prototype);
    }

    public DateDeleted { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM