sirbuckets
sirbuckets

Reputation: 1

Copying base class in derived class constructor

Is there a way to copy an object fields to a base class in a derived class constructor without having to individually copying every field?

Example:

public class A
{
    int prop1 { get; set; }
    int prop2 { get; set; }
}

public class B : A
{
   public B(A a)
   {
      //base = a; doesn't work.
      base.prop1 = a.prop1;
      base.prop2 = a.prop2;
   }
}

A a = new A();
B b = new B(a);

Upvotes: 0

Views: 1591

Answers (5)

Grant Winney
Grant Winney

Reputation: 66439

It sounds like you want to add all properties from A to B without having to specify them all separately. If you don't want to have to keep adding new ones to the constructor, you could use reflection to do the work for you.

public B(A a)
{
    var bType = this.GetType();

    // specify that we're only interested in public properties
    var aProps = a.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

    // iterate through all the public properties in A
    foreach (var prop in aProps)
    {
        // for each A property, set the same B property to its value
        bType.GetProperty(prop.Name).SetValue(this, prop.GetValue(a));
    }
}

A few notes about this:

  • The above code sets public instance properties, so you'd need to change your properties in A to be public.
  • I'd only consider this safe because you know that B contains everything in A (since it is derived from it).
  • If you only have a few properties, especially if they don't change often, just list them individually... it'll be easier to see exactly what your code is doing.

Upvotes: 1

MikeDub
MikeDub

Reputation: 5283

If you really want to do this and cannot access the properties via inheritance then you can do via reflection like this:

public class Aclass
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
}

public class Bclass : Aclass
{
    public Bclass(Aclass aInstance)
    {
        CopyPropertiesFromAltInstance(aInstance);
    }

    public void CopyPropertiesFromAltInstance(Aclass aInstance)
    {
        PropertyInfo[] aProperties = aInstance.GetType().GetProperties();
        PropertyInfo[] myProperties = this.GetType().GetProperties();
        foreach (PropertyInfo aProperty in aProperties)
        {
            foreach (PropertyInfo myProperty in myProperties)
            {
                if (myProperty.Name == aProperty.Name && myProperty.PropertyType == aProperty.PropertyType)
                {
                    myProperty.SetValue(this, aProperty.GetValue(aInstance));
                }
            }
        }
    }
}

Upvotes: -1

D Stanley
D Stanley

Reputation: 152521

The members are private, so you can't access them from even a derived class. Even if they were protected, you still couldn't access them on an instance of A from the B class.

In order to do this without reflection, the members will have to be public:

public class A
{
    public int prop1 { get; set; }
    public int prop2 { get; set; }
}

// Define other methods and classes here
public class B : A
{
   public B(A a)
   {
      //base = a; doesn't work.
      base.prop1 = a.prop1;
      base.prop2 = a.prop2;
   }
}

Upvotes: 0

Jay
Jay

Reputation: 82

public class A
{
    public A(A a)
    {
       prop1 = a.prop1;
       prop2 = a.prop2; 
    }

    int prop1 { get; set; }
    int prop2 { get; set; }
}

public class B : A
{

   public B(A a) : base (a)
   {

   }
}

A a = new A();
B b = new B(a);

Something like this, although I'm not sure if it is syntactically correct because I didn't compile it. You should use the base keyword after the child class's constructor to pass the values of it's dependencies to the base class.

Edit: But I just realized that you are passing a base class to a child class. And this is a design flaw.

Upvotes: 0

kurasa
kurasa

Reputation: 5323

I can't for the life of me understand why you want to do this

You are passing an instance of Base class into the constructor of a derived class. What are you trying to do?

have you tried this = a instead of base = a?

Upvotes: -1

Related Questions