This is easier to explain with an example. Given these two classes:
public class MyClassA { public String Property_A { get; set; } public String Property_B { get; set; } public String Property_C { get; set; } public String Property_D { get; set; } ... public String Property_Y { get; set; } } public class MyClassB: MyClassA { public String Property_Z { get; set; } }
Suppose I have a fully created instance of MyClassA (with all properties from AY populated). Then I need to make an instance of MyClassB, which is exactly the same as my instance of MyClassA, but with a filled Property_Z (with a custom value, of course). How can i do this?
Doing this does not work (throws and discards the Cast exception):
MyClassB myInstanceB = (myClassB) myInstanceA; myInstance.Property_Z = myCustomValue;
I didnβt have to do anything like this from my C ++ days, and I'm at a dead end.
Any ideas?
UPDATE: I found a solution to my problem in the way I create instances. This is below. I did not mark this as an answer, because it did not quite correspond to my question.
source share