Yaxdraz
Yaxdraz
Explore posts from servers
CC#
Created by Yaxdraz on 8/28/2023 in #help
❔ 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" });
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?
2 replies