4

I have two classes which should be exactly the same apart from 1 class needed another property.

Instead of re-writing all of the properties twice, I thought of inheriting all of the properties from BaseClass with just the one extra property in MyNewClass

 public class BaseClass
    {
        public int BaseProperty1 { get; set; }
        public int BaseProperty2 { get; set; }
        public int BaseProperty3 { get; set; }
    }

    public class MyNewClass: BaseClass
    {
        public int? ExtraProperty{ get; set; }
    }

Since I already fill in all of the details for the original BaseClass in my function, It would be far easier to be able to use this instance of the class to fill in the details of the new instance of MyNewClass.

I hoped it would be as simple as the following, but unfortunately I get the error: System.InvalidCastException: 'Unable to cast object of type 'BaseClass' to type 'MyNewClass'.'

 MyNewClass myNewClass= new MyNewClass();
 myNewClass = (MyNewClass)baseClass; //baseClass is alread populated at this point
 myNewClass.ExtraProperty = 1;

Is there any way to quickly populate a class using another class which has one less property?

I could just set each property individually, but the class which I am using is quite large and it feels like bad practice.

Thanks in advance for any help.

James Tordoff
  • 661
  • 1
  • 5
  • 25
  • Possible duplicate of [Apply properties values from one object to another of the same type automatically?](https://stackoverflow.com/questions/930433/apply-properties-values-from-one-object-to-another-of-the-same-type-automaticall) – eocron Nov 21 '18 at 16:48
  • Hence the name, answers in linked question is type-independand, so you can copy baseClass to myNewClass regardless. – eocron Nov 21 '18 at 16:49
  • PropertyCopy is a handy method, but in the above scenario, i would roll with the constructor solution. It might be more clear / better readable. Also if `MyNewClass` has only one constructor, a developer is forced to use it. ... out before 'opinion based' close votes ;D – nilsK Nov 21 '18 at 16:54
  • See also https://stackoverflow.com/questions/9885644/cast-the-parent-object-to-child-object-in-c-sharp/9885823#9885823 – Avner Shahar-Kashtan Nov 21 '18 at 17:28

3 Answers3

5

Not every fruit is an apple, so from compiler perspective not every BaseClass is an instance of MyNewClass hence the cast fails.

There are couple of things you can do. For example use constructor to populate values:

public class MyNewClass : BaseClass
{
    public int? ExtraProperty { get; set; }

    public MyNewClass(BaseClass baseClass)
    {
        BaseProperty1 = baseClass.BaseProperty1;
        BaseProperty2 = baseClass.BaseProperty2;
        BaseProperty3 = baseClass.BaseProperty3;
    }
}

Then you can do:

 var myNewClass = new MyNewClass(baseClass);
 myNewClass.ExtraProperty = 1;
Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

If you have a lot of properties and don't want manually set every each of them then I suggest you iterate through them like this

public class MyNewClass : BaseClass
{
    public MyNewClass(BaseClass seizeProperties)
    {
        PropertyInfo[] baseProperties = typeof(BaseClass).GetProperties();
        foreach (PropertyInfo property in baseProperties)
        {
            property.SetValue(this, property.GetValue(seizeProperties));
        }
    }
    public int? ExtraProperty { get; set; }
}
Max Jacobi
  • 163
  • 11
0

"I already fill in all of the details for the original BaseClass in my function"
So, if you have func like

void your_func_fill(BaseClass _BaseClass)

you can just call this func with child class object

MyNewClass _MyNewClass;

...

your_func_fill(_MyNewClass)

Good luck!

AndrewF
  • 33
  • 3