Using a Type Object to Create a Common

I am trying to instantiate a generic class using an object of type.

Basically, at runtime I will have a collection of objects of different types, and since there is no way to find out what types they will be exactly, I think I will have to use Reflection.

I was working on something like:

Type elType = Type.GetType(obj); Type genType = typeof(GenericType<>).MakeGenericType(elType); object obj = Activator.CreateInstance(genType); 

This is good and good. ^ ___ ^

The problem is that I would like to access the method of my GenericType <> instance, which I cannot, because it was introduced as an object class. I can’t find a way to do this obj in a specific GenericType <> because it was a problem in the first place (i.e. I just can’t add something like :)

 ((GenericType<elType>)obj).MyMethod(); 

How to solve this problem?

Many thanks! ^ ___ ^

+7
generics reflection c #
source share
7 answers

You need to continue using Reflection to call the actual method:

 // Your code Type elType = Type.GetType(obj); Type genType = typeof(GenericType<>).MakeGenericType(elType); object obj = Activator.CreateInstance(genType); // To execute the method MethodInfo method = genType.GetMethod("MyMethod", BindingFlags.Instance | BindingFlags.Public); method.Invoke(obj, null); 

See Type.GetMethod and MethodBase.Invoke for more information.

+5
source share

Once you start the reflection game, you must play it to the end. The type is not known at compile time, so you cannot use it. You will need to call the method by reflecting:

 obj.GetType().InvokeMember( "MyMethod", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null ); 
+4
source share

In C # 3.5, you have to use Type.GetMethod and MethodInfo.Invoke to call a method.

In C # 4, you can use a dynamic keyword and bind to a method at runtime.

+3
source share

The simplest approach is to extract a non-generic universal supertype (base class or interface) from GenericType that contains the methods you want to open for this purpose:

 class GenericType<T> : GenericBase { ... } class GenericBase { abstract void MyMethod(); } 

Otherwise, use reflection to access the method itself, as suggested by @Aaronaught.

+2
source share

Once you have created the instance, simply do:

 MethodInfo method = genType.GetMethod("MyMethod"); method.Invoke(obj, null); 
+1
source share

If you know the signature of the methods you want to call, you can not only use MethodInfo.Invoke() , as shown in other examples here, but also create a delegate that allows a more efficient call (if you need to call the same method several times) with using Delegate.CreateDelegate() .

0
source share

I'm not sure how much your types change or whether you have control over the methods that you call them, but it can be useful to create an interface that determines which set of functions you are going to use to call. Therefore, by creating an instance, you can apply it to the interface and call any functions that you need.

create your own standard interface (which you need to implement in each type, if you have control over them):

 interface IMyInterface { void A(); int B(); } class One : IMyInterface { ... implement A and B ... } Type elType = Type.GetType(obj); Type genType = typeof(GenericType<>).MakeGenericType(elType); IMyInterface obj = (IMyInterface)Activator.CreateInstance(genType); obj.A(); 
0
source share

All Articles