C#C
C#3y ago
Guts

❔ Using variables in generic methods

I code the following code but Business.Get is an async function:
Type businessType = typeof(Business);
MethodInfo methodInfo = businessType.GetMethod("Get");

Type type = null;
if (entityType == EntityTypes.Seasons) type = typeof(Seasons);
else if(entityType == EntityTypes.Leagues) type = typeof(Leagues);

MethodInfo constructedMethod = methodInfo.MakeGenericMethod(type); 
object instance = Activator.CreateInstance(businessType);

List<Seasons> dataSource = (List<Seasons>) constructedMethod.Invoke(instance, new object[] { 1, "0" });

What I want is to define generic as variable in Business.Get method but i actually have no idea if it will work.
Can you help me?
Was this page helpful?