© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
11 replies
dreadfullydistinct

Converting from a string to a generic method invocation

Looking for design patterns to help solve this problem I’m having at work. The situation is that we have a URL like GET /entities/<userid>/<entity type> where entity type is a string, and we want to fetch the entities of that type belonging to the current user.

Normally I would just use a switch case to go from eg entitytype = “someentity” => entitiesService.GetEntities<SomeEntity>(). But my tech lead insists that new entity types should be automatically supported without code changes to this endpoint because he thinks it will be forgotten.

I’ve landed on a solution using reflection to grab the types deriving from my base Entity class and build a dict of (string, Type), then using reflection to get the method info of GetEntities and use MakeGenericMethod with the type from the dict, but am not too pleased with it.

I’m considering the following alternatives:
- source generators, seemed like a lot of code to generate 1 switch case
- using reflection in the testing to make the pipeline fail if another dev forgets to update the endpoint

But I’d like to know if there are any other solutions people can think of / thoughts on some of the existing options
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

✅ Specialising a generic method
C#CC# / help
4y ago
✅ Converting string to DateTimeOffset
C#CC# / help
2y ago
❔ Converting from string into hex literal
C#CC# / help
3y ago