c# - Converting child class to parent -


i have 2 classes product , extendedproduct. extendedproduct derived product class. there more fields in extendedproduct class.

the problem when cast extendedproduct product product object has fields extendedproduct has.

i want convert extendedproduct product class without extendedproduct class' field appearing in product class.

as long extendedproduct product casting latter won´t change casted instance because reference same object. need new instance of product, e.g. using copy-constructor:

class product {     public product(product p) {         this.myprop = p.myprop;        } } 

now can call like:

var product = new product(myextended); 

however sounds quite weird me. why want "delete" extended properties @ all? you´ll need extra-information provided earlier. can cast parent-class not provide properties @ all:

var p = (product) myextendedproduct; 

now although p instance of extendedproduct can access properties product. basic priciple of polymorphism.


Comments