I am primarily a Java programmer, so this will be one of those issues that Java has, equivalent to C #. Thus, in Java, you can restrict the type argument at compile time to extend some superclass, for example:
public <T extends BaseClass> void foo(Class<T> type) { ... }
and even
public <T extends BaseClass> T foo(Class<T> type) { ... }
You can even bind several interfaces:
public <T extends BaseClass & BaseInterface1 & BaseInterface2> void foo(Class<T> type) { ... }
How is this done in C #? I know that you can use "where T: BaseClass", but this only applies when you have an instance of T. What happens when you have only an instance of a type?
EDIT:
To explain, here is what I would like to do:
ASSEMBLY No. 1 (base.dll):
abstract class BaseClass { abstract void Foo(); }
ASSEMBLY # 2 (sub1.dll, base.dll links):
class SubClass1 : BaseClass { void Foo() {
ASSEMBLY # 3 (sub2.dll, base.dll links):
class SubClass2 : BaseClass { void Foo() {
ASSEMBLY No. 4 (main.dll, base.dll links):
class BaseClassUtil { static void CallFoo(Type<T> type) where T : BaseClass { T instance = (T)Activator.CreateInstance(type); instance.Foo(); } } public static void Main(String[] args) {
So, in this example, I donβt care which class a type variable represents if it is derived from BaseClass, so as soon as I create an instance, you can call Foo ().
Parts that are not C # vaild code (but rather some Java layout) are the "generic" classes Type: Type <T> and Type <?: BaseClass>.