C#在父(抽象)类中更改子类的类型

时间:2012-04-19 12:20:15

标签: c# inheritance

代码:

abstract class Parent{
    void changeChild(){

    }
}
class Child1: Parent{
}
class Child2: Parent{
}
//-----
Parent parent = new Child1(); //instantiated as Child1
parent.changeChild(); //change type of this class to Child2

所以父级应该是Child2类的实例。

据我所知,Child1可能与Child2不同(更多/更少的字段,方法), 但我只是想在这个对象上调用构造函数,这是不允许的。

简单

parent = new Child2();

可以完成,但有10个子类(成长),我想把它移到父类

这在c#中是否有可能?

由于

6 个答案:

答案 0 :(得分:1)

您无法更改现有对象的类型,但可以创建新对象并将其返回。

示例:

abstract class Parent{

  Parent ChangeChild<T>() where T : Parent {
    if (typeof(T) == typeof(Child1)) {
      return new Child1(this);
    if (typeof(T) == typeof(Child2)) {
      return new Child2(this);
    } else {
      throw new NotImplementedException("Unhandled type");
    }
  }

}

class Child1: Parent{
  public Child1() {} // create
  public Child1(Parent source) {} // convert
}

class Child2: Parent{
  public Child2() {} // create
  public Child2(Parent source) {} // convert
}

Parent parent = new Child1();
parent = parent.ChangeChild<Child2>();

答案 1 :(得分:0)

好吧,如果你想要那个ctor叫做automaticcally,应该足够wtrite的东西,比如

class Child1: Child2{
}

class Child2: Parent{
}

因此,当Child2构建时,您将调用Child1 ctor。但是如果你的话,这是完全不同的架构设计。

如果不是你要求的那些,请澄清。

答案 2 :(得分:0)

Automapper可能是您最好的选择。

T changeChild<T>() : Parent{
   // change the type here.
}

答案 3 :(得分:0)

这样的事情会起作用吗?

abstract class Parent {
    private Parent Proxy;
    public void ChangeChild<T>() where T : Parent, new() {
        Proxy = new T();
    }
}

您必须使用Proxy对象来调用成员和属性。

答案 4 :(得分:0)

你不能这样做。也许你可以创建Parent作为包装类

class Parent
{
    Parent _child;

    public Parent(Parent child)
    {
        _child = child;
    }

    public void ChangeChild(Parent child)
    {
        _child = child;
    }

    public string AProperty {
        get { return _child.AProperty; }
        set { _child.AProperty = value; }
    }

    public int AMethod(int x)
    {
        return _child.AMethod(x);
    }
}

然后

Parent parent = new Parent(new Child1());
parent.AProperty = "hello";
int y = parent.AMethod(55);

parent.ChangeChild(new Child2());

注意:家长不应该了解优秀OO设计中的特定孩子。这样可以确保您以后可以创建新的子项(例如Child3),而无需更改Parent

但是,如果这不是您的问题,并且您希望父级自动关注更改子级,则使用

更改或重载构造函数
    public Parent()
    {
        _child = new Child1();
    }

并使用

更改或超载ChangeChild
    public void ChangeChild()
    {
       if (_child is Child1) {
           _child = new Child2();
       } else {
           _child = new Child1();
       }
    }

答案 5 :(得分:0)

如果需要在运行时更改某个对象的类型,则不是继承的情况。你应该在这里使用组合。尝试类似Strategy的内容(如果Parent类代表某些行为)。

public class Foo
{
    // provide default value (or inject it via ctor)
    private Parent _parent = new Child1(); 

    public void ChangeChild(Parent parent){
        _parent = parent;
    }

    public void Bar()
    {
        _parent.DoSomething();  
    }
}

public abstract class Parent
{
    public abstract void DoSomething();
}

public class Child1: Parent
{
    public override void DoSomething() { ... }
}

public class Child2: Parent
{
    public override void DoSomething() { ... }
}

现在您可以在运行时更改依赖类型:

Foo foo = new Foo(new Child1());
foo.Bar(); // child1 implementation used
foo.ChangeChild(new Child2());
foo.Bar(); // child2 implementation used