public class a{
public string x1 {get;set;}
public string x2 {get;set;}
public string x3 {get;set;}
}
public class b:a{
}
Obviously var foo = (b)new a(); will throw a casting error at runtime.
The only other way I can think to assign all the properties of an already instantiated and populated a is to manually copy each one over into a fresh instance of b.
Is this correct?
This type of cast is wrong, because you can’t cast parents to their children.
Type of
adoesn’t know about metainformation of typeb. So you need provide the explicit cast operator to do such things, but in this case you must remove inheritance.Other option is to define some interface, such as in other questions.
More information:
http://msdn.microsoft.com/en-us/library/ms173105.aspx
http://msdn.microsoft.com/en-us/library/85w54y0a.aspx