Verify that the instance instance represents a type assigned from a specific class

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() { // some code } } 

ASSEMBLY # 3 (sub2.dll, base.dll links):

 class SubClass2 : BaseClass { void Foo() { // some other code } } 

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) { // Here I use 'args' to get a class type, // possibly loading it dynamically from a DLL Type<? : BaseClass> type = LoadFromDll(args); // Loaded from DLL BaseClassUtil.CallFoo(type); } 

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>.

+8
java c # types extends
source share
2 answers

No, at compile time there is no way to force Type to be assigned to a universal type. If I understand correctly, then you want:

  void Foo<T>(Type type) { ... } //compile time error if an instace typed `type` is not assignable to `T`. 

It means:

  void Foo<IFormattable>(typeof(string)); //ok void Foo<IDisposable>(typeof(string)); //compile time error 

Obviously, at runtime it is trivial, but the language does not support this at compile time.

+2
source share

From what I understand, you are saying a general type constraint

 public void Foo<T>(Type type) where T:BaseClass, BaseInterface1, BaseInterface2 { //your code } 

Here's another article: Type Parameter Limitations (C # Programming Guide)

When you define a generic class, you can apply restrictions to types of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate a class using a type that is not allowed by the constraint, the result is a compile-time error.

EDIT:

Here is your example. Now, if you try to call BaseClassUtil.CallFoo<T> with something other than BaseClass and its derived classes, you will get a compilation error. Here is a complete example in dotNetFiddle . So, the tricky part - this restriction of your class must occur in the Util class

  public static void Main(string[] args) { //so your LoadFromDll method should return Type. Type doesn't have generic implementation ! Type type = typeof(SubClass1); BaseClassUtil.CallFoo<BaseClass>(type); Type type2 = typeof(SubClass2); //you can write BaseClassUtil.CallFoo<SubClass2>(type2); if you want BaseClassUtil.CallFoo<BaseClass>(type2); } public class BaseClassUtil { public static void CallFoo<T>(Type type) where T : BaseClass { T instance = (T)Activator.CreateInstance(type); instance.Foo(); } } public class TestClass { public int ID { get; set; } } public abstract class BaseClass { public abstract void Foo(); } public class SubClass1 : BaseClass { public override void Foo() { Console.WriteLine("SubClass 1"); } } public class SubClass2 : BaseClass { public override void Foo() { Console.WriteLine("SubClass 2"); } } 
+2
source share

All Articles