I'm trying to design an API that is supposed to have a certain method for each of the 20+ concrete classes that I need to support (I can't change those specs).
My first idea was to use overloading to have the same method with different parameters and being able to automatically invoke the correct one based on the concrete class, however I'm getting the error in the title and I was wondering if there was a better way to do this (without having to use if/switch on object's type).
Here's a quick pseudo-code example:
class BaseClass {}
class ChildClassA : BaseClass {}
class ChildClassB : BaseClass {}
class Factory {
public static void Build(ChildClassA obj){}
public static void Build(ChildClassB obj){}
}
class Main {
public static void Main(string[] args){
BaseClass obj = getFromSomewhereRemote();
Factory.Build(obj); // This is the line where I get the error
}
}
I understand that I could build a method like this one:
void Build(BaseClass obj){
switch (obj){
case BaseClassA objA:
Build(objA);
break;
....
}
}
but I was wondering if I could have the same results without switching on types.