Is there a way to store different types (names of types) in a List (or dictionary or tuples) and iterate the list to call a generic method for each item in the list?
You can create a list of System.Type
which stores all the different types you want to call the generic method with.
The answer can change based on your exact requirements, but assuming you are trying to call a public method for an instance of a class, you can do something like the following:
var types = new List<System.Type>(); // store all types in this
var genericMethod = instanceOfClassWithGenericMethod.GetType().GetMethod("MethodNameHere"); // you can use `nameof` for method name
foreach (var type in types)
{
var genericMethodWithType = genericMethod.MakeGenericMethod(type);
genericMethodWithType.Invoke(instanceOfClassWithGenericMethod, null);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.