I have the following class hierarchy:
class A {
/// stuff
}
class B : A {
/// stuff
}
class C<T> : B {
/// stuff
}
Then somewhere completely different, I have the following three methods:
public void foo(A a) {
}
public void bar(B b) {
}
public void bar<T>(C<T> ct) {
}
Now for some reason I need to call the "right" panel from foo, given the actual type A. That is, if A really belongs to type B, I need to call overload 1, and if A is really type C (whatever T ), I need to call overload 2. And for completeness, if A is neither B nor C, do nothing.
I am currently using the IsAssignableFrom method of the Type class to decide if conversion to B is possible:
public void foo(A a) {
if (typeof(B).IsAssignableFrom(a)) {
bar((B)a);
}
}
But this also applies to options C. So, the question is, how can I complete this survey? Reflection? dynamics? We use .NET 4, so everything that was introduced in C # 5, I can not use.