© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
7 replies
Gopher

Is there a good way to use a object from Type Type in a generic method?

  Type type = typeof(object);
  GenericMethod</*use Type here*/>(); 
  Type type = typeof(object);
  GenericMethod</*use Type here*/>(); 


I have a lot of serialized different Objects in a Database along with their typename as string. All of them share the same Interface. Now i want to deserialize all of those to the right Objects.
Therefore i created a Application scanner that collects the Types for all those objects. Now i want to compare the typenames and deserialize them in a loop.
All of this feels wrong.

private IInterface GetObject(StoredObject storedObject)
        {
            foreach(var type in TypeList)
            {
                if(type.Name == storedObject.Type)
                    return JsonConvert.DeserializeObject</*TheCorrectType*/>(storedObject.ActualObjectData);
            }
        }
private IInterface GetObject(StoredObject storedObject)
        {
            foreach(var type in TypeList)
            {
                if(type.Name == storedObject.Type)
                    return JsonConvert.DeserializeObject</*TheCorrectType*/>(storedObject.ActualObjectData);
            }
        }

sth. like this
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

Is there a way to call generic methods through Type (from typeof or GetType) (without reflection)
C#CC# / help
4y ago
✅ is there a way to get the method name from Task?
C#CC# / help
3y ago
❔ generic class in generic method
C#CC# / help
3y ago
Getting the typeof this to use in a generic method
C#CC# / help
2y ago