C
C#

help

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

D.deado10/19/2022
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 the storedObject contains the typename as string and the actual object as string
Mmtreit10/19/2022
There is a non generic overload that takes the Type object as a method parameter. If you can have a mapping of strings to the actual type objects (like a dictionary or even just a switch expression) you can use that maybe?
AAngius10/19/2022
Generics need to be known at compile time
Mmtreit10/19/2022
Not if you're willing to write very gnarly reflection code when
AAngius10/19/2022
Shhh
Mmtreit10/19/2022
But yes generally don't do that
D.deado10/19/2022
Thx.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Git Moving from feature branch to main [Answered]Hi! I have made a new branch for a new feature and have made some commits in there, how can I push tSlash Command purge [Answered]Hi, I'm trying to use my old purge command as a slash command, but I end up giving me the Discord Erwait for processes [closed]I want to wait for a process to close to perform some action how can I do that?Collection was modified enumeration operation may not executeHi - I am working on a client - server chat application for my school project. The server app has a how i can write words in arrays [Answered]i wanna write words after numbersC Sharp MonorepoHey guys 👋 I want to create an Monorepo for my C# Backend and Frontend (Blazor). Any idea, how canReturn a custom ProblemDetails from a controller. How should I do it?There is BaseController.Problem but that creates a new problem details with some defaults. ShoulDon’t understand nullable reference typesGet Environment Variables...I am trying to get the connection string for my Azure Tables with the following code. TabHow to find the implementation of Math. functionsI think i've heard mathematical functions of `System.Math` is implemented in external libraries writMaking concrete object classes```cs private void Initialize_NavigationItems() { var fooNavigationItem SignalR Get Client State From Another Service.Hello, I have a SignalR client, and a method in another service that get called by the client with aSetting the id of an object to property in a nested Object on creationHey devs, I have a little tricky one this time, which is possibly very simple to implement. so I havEF Core many-to-many relation errorThis is the error I am getting : And I don't really know how to fix it. These are my modals ```cshHow do I simulate the logs I keep?I have a data stream in a winframe interface. I keep these logs somewhere and when I press the simulHow do i publish an asp.net Core applicationI can't use `dotnet publish` as i use COM references, but i need a release build of my aspnetcore pr