© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
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" });
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Fluent Syntax - generic methods
C#CC# / help
2y ago
❔ Using methods in Grade Statistics project
C#CC# / help
4y ago
having issues carrying variables to methods
C#CC# / help
4y ago