How to call a generic method via Type variable in C#?
Hello everyone. I'm writing a small parser that should handle different data types, and I've encountered a problem that I haven't been able to solve for two days now.
I have a generic method:
```csharp
public T Deserialize(string data) where T : class
{
// some deserialization logic
return JsonConvert.DeserializeObject(data);
}
```
At runtime, I receive a `Type` of the object that needs to be deserialized. For example, via reflection I find all classes implementing the `IEntity` interface, and for each one I want to call `Deserialize` with a specific type.
The problem is that I can't just write `Deserialize(data)` because `myType` is a variable of type `Type`, not a type name at compile time. I tried using `typeof` and `MakeGenericMethod`, but I couldn't get it to work.
No answers yet.
Log in to leave an answer
Blogs