The traditional way to do this is to invert what you are asking and to use virtual
functions to implement polymorphism
in the fashion described by @RuiJarimba in the comments. For example, consider these three classes (I renamed your ObjectX classes to ClassX):
public abstract class ClassA
{
public abstract void DoSomething();
}
public class ClassB : ClassA
{
public override void DoSomething()
{
//Do something that is ClassB specific
}
}
public class ClassC : ClassA
{
public override void DoSomething()
{
//Do something that is ClassC specific
}
}
Every instance of ClassB
and ClassC
is inherently a ClassA
(through inheritance). Both ClassB
and ClassC
implement a DoSomething
method that overrides the abstract definition that ClassA
has defined. Now, I can take any ClassA
object (which has to be an instance of a sub-class of ClassA
since ClassA
is abstract) and call DoSomething()
on it and get a class-specific implementation of DoSomething()
. In the code below, I create separate instances. In real life, you normally have a collection of ClassA
object references that you walk through calling appropriate methods:
ClassA objectB = new ClassB();
ClassA objectC = new ClassC();
objectB.DoSomething(); //calls ClassB's version
objectC.DoSomething(); //calls ClassC's version
It's not exactly what you are asking about, but I think it's what you want.
Continuing On
If you want to keep that same function signature, you can add the following to the ClassA
definition:
public static void DoSomething<T>(T data) where T : ClassA
{
data.DoSomething(); //dispath through the right class using a virtual function
}
If you do that, then this will work (using the objectB
and objectC
instances created above:
ClassA.DoSomething(objectB); //calls ClassB's version
ClassA.DoSomething(objectC); //calls ClassC's version
What the new code does is take the object it is passed (which is necessarily an instance of a ClassA
subclass) and simply calls the previously defined virtual function, dispatching things to the appropriate class's implementation.