C
C#16mo ago
Yaxdraz

❔ 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?
1 Reply
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server