How to set a generic variable of type Type in a loop?

I would like to do some similar process in a loop like this, calling a generic method with different types.

AAA , BBB - all classes. CreateProcessor is a common method in the MyProcessor class.

 new List<Type> {typeof (AAA), typeof (BBB)}.ForEach(x => { var processor = MyProcessor.CreateProcessor<x>(x.Name); processor.process(); }); 

This does not compile, I got a Cannnot resolve symbol x error message.

Technically, how to achieve it? (I know the strategy is better ...)

+6
source share
4 answers

Sorry, I updated my question. I intended to call the general method actually.

 var method = typeof(MyProcessor).GetMethod("CreateProcessor", new Type[] { typeof(string) }); new List<Type> { typeof(AAA), typeof(BBB) }.ForEach(x => { dynamic processor = method.MakeGenericMethod(x).Invoke(null, new[] { x.Name }); processor.process(); }); 
+7
source

Reflection is required to work with the Type class:

  new List<Type> { typeof(AAA), typeof(BBB) }.ForEach(x => { var type = typeof(MyClass<>).MakeGenericType(x); dynamic processor = Activator.CreateInstance(type, x.Name); processor.process(); }); 
+8
source

Here's how you could create a new variable using only type information, using dynamic , you can call any method that you know exists for all types. I would suggest (assuming these types are your own classes) that you implement the baseclass interface or something for them, if possible, this simplifies your pretty much ...

 new List<Type> { typeof(string), typeof(int) }.ForEach(x => { dynamic processor = Activator.CreateInstance(x); processor.ToString(); processor.CallAnyMethodHere(); processor.Process(); }); 

Edited Code - Add a Clear Example

 using System; using System.Collections.Generic; using System.Reflection; using System.Text; public class mainClass { public static void Main(string[] args) { new List<Type> { typeof(StringBuilder), typeof(Int64) }.ForEach(x => { dynamic instance = Activator.CreateInstance(x); DoSomething(instance); }); Console.ReadKey(); } public static void DoSomething(StringBuilder stringBuilder) { Console.WriteLine("StringBuilder overload"); } public static void DoSomething(Int64 int64) { Console.WriteLine("Int64 overload"); } } 

Edit 2 - calling only the general method

 using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.Linq; public class mainClass { public static void Main(string[] args) { new List<Type> { typeof(StringBuilder), typeof(Int64) }.ForEach(x => { var methodInfoArray = typeof(mainClass).GetMethods(); var methodInfo = methodInfoArray.First(mi => mi.Name == "DoSomething" && mi.IsGenericMethodDefinition); var genericMethod = methodInfo.MakeGenericMethod(new Type[] { x }); var blah = genericMethod.Invoke(null, new object[] { "Hello" }) as MethodInfo; }); Console.ReadKey(); } public static void DoSomething<T>(string variable) { Console.WriteLine("DoSomething<T> " + typeof(T) + " overload - " + variable); } public static void DoSomething(string variable) { Console.WriteLine("DoSomething - " + variable); } } 
0
source

You must use reflection to create such a common object. This Microsoft page has excellent information on what you need to do: A practical guide. Studying and creating generic types with reflection

0
source

All Articles