简体   繁体   中英

call constructor with instanciated superclass in c#

I have this construct.

public class SuperClass{}
public class InheritedClass : SuperClass{}

I want my instantiated Superclass to become an InheritedClass instance.

var superClassInstance = new SuperClass();
InheritedClass inHeritInstance = new InheritedClass(superClassInstance) ;

Somebody told me, that will not work without setting all properties in the Constructor. Is that correct?

I want my instantiated Superclass to become an InheritedClass instance.

This is not possible. If you want it to be an instance of InheritedClass you must create one. You cannot upcast an instance of SuperClass .

That's true.. you can not Assign directly, In Constructor you have to transfer.

Instead..

you should create interface and implement the Interface into the class from where you are passing the value.

Now in into InheritedClass create constructor with the the interface as parameter, so whatever values are assigned into the First class, will be passed into InheritedClass

here you will have full access of First Class property.

Refer below dotnetfiddle url.

http://dotnetfiddle.net/8AgOCF

Yes, that is correct. There is no inbuilt way to provide your own baseclass object to promote to subclass. Each subclass will build it's own baseclass before it calls the subclasses' constructor on it.

You will need to write your own constructor in the subclass that copies all relevant data.

In a sense, yes, I'm afraid that is correct. A constructor is, by definition, creating a new instance. So anything from an existing instance is going to have to be copied to the new instance. It might be something like this:

public InheritedClass(SuperClass superClassInstance)
{
    // base fields
    Field1 = superClassInstance.Field1;
    Field2 = superClassInstance.Field2;
    Field3 = superClassInstance.Field3;

    // local fields
    Field4 = "some value";
    // etc.
}

Or you can split the work between the classes, something like this:

public SuperClass(superClassInstance)
{
    Field1 = superClassInstance.Field1;
    Field2 = superClassInstance.Field2;
    Field3 = superClassInstance.Field3;
}

then in the derived class:

public InheritedClass(superClassInstance)
    : base(superClassInstance)
{
    Field4 = "some value";
    // etc.
}

It gets a lot easier the other way around, creating a SuperClass from InheritedClass , since any instance of InheritedClass is an instance of SuperClass and can polymorphically be interpreted as such:

SuperClass someObject = inheritedClassInstance as SuperClass;

(Note that this would not be a "new instance" so watch out for reference errors.) You might be able to do the same in reverse:

InheritedClass someObject = superClassInstance as InheritedClass;

But there is no guarantee that superClassInstance is also an instance of InheritedClass so you'd want to check for errors here. And, again, this is not a new instance.

Using composition instead of inheritance you can potentially achieve your goal with less code. For example, if you have a class like this:

public class InheritedClass
{
    public SuperClass BaseObject { get; private set; }
    // other fields
}

Then you can include a constructor in InheritedClass which just sets that one object:

public InheritedClass(SuperClass superClassInstance)
{
    BaseObject = superClassInstance;
}

Again, however, note that this is not a new instance of SuperClass in the property but a reference to the existing one. To ensure a new one you'd still have to write your manual code (possibly in a .Copy() or .Clone() method on SuperClass ?) to duplicate the instance.

In short, after all of my directionless brainstorming, cloning an instance field-by-field is going to require putting that field-by-field code somewhere .

Ok, here (a and b fields are for demo purposes):

public class SuperClass
{
    object a;
    public SuperClass()
    {
        a = "123";
    }
    // you need to have this
    public SuperClass(SuperClass copy)
    {
        a = copy.a;
    }
}

public class InheritedClass : SuperClass
{
    object b;
    public InheritedClass()
    {
        Init();
    }
    // and this
    public InheritedClass(SuperClass super): base(super)
    {
        // bacause you can't call base() and this()
        Init();
    }

    private void Init()
    {
        b = "456";
    }
}

So you can do

var super = new SuperClass();
var inherited = new InheritedClass(super);

Proof .

I found a solution, which can at least copy all the properties automatically.

public InheritedClass(SuperClass superClassInstance)
{
 foreach(var currentItem in superClassInstance.GetType().GetProperties())
  {
    GetType().GetProperty(currentItem.Name).SetValue(this,currentItem.GetValue(superClassInstance,null),null);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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