No, this is not possible: suppose you call addProduct for a variable of class A that points to an instance of class C , passing in an object that is ProductBase , but not a SomeProduct . A past instance cannot be converted to SomeProduct , so it will not be compiled in the first place.
Closest to this is a solution with generics:
public abstract class A<T> where T : ProductBase { protected abstract void addProduct(T p); }
Then you can define your class C as follows:
public class C : A<SomeProduct> { protected override void addProduct(SomeProduct p); }
Of course, this means that you need to enter any type A variables in SomeProduct , as in A<SomeProduct> = new C(); .
You cannot simply have a variable of type A (which provides the addProduct method) without specifying the actual value of an argument of type T On the other hand, calling the addProduct method addProduct not be possible in your solution, as described above.
Now you can enter a non-strongly typed method:
public abstract class A<T> where T : ProductBase { protected abstract void addProduct(T p); protected void addProductUntyped(ProductBase p) { T typedProduct = p as ProductBase; if (typedProduct != null) { addProduct(typedProduct); } else { throw new ArgumentException("p has an incompatible type."); } } }
However, such a solution is usually really needed only when there are some users of the class who can know the general type, while others do not.
OR mapper
source share