I have a Json deserialiser class that is created using a generic parameter like this:
Deserializer<T> results = Deserializer<T>.FromFile(file);
Where T is the interface that an arbitrary number of types implement.
I'd like T to be chosen by the name of the class in the form of a string such as "Person" and the class Person implements T.
So it could be written like this:
Deserializer<Type.GetType("Person")> results = Deserializer<Type.GetType("Person")>.FromFile(file);
I have tried exactly this above and it gives me an error:
Using the generic type 'Deserializer<T>' requires 1 type arguments
I've also looked at this
but trying to use some of these resulted in other errors such as:
randomVariable is a 'field' but is used like a 'type'
Is there a mistake in my code or am I going about this the wrong way?
How can I get this to work?
Solution My final code was
Type genericType = typeof(Deserializer<>);
Type[] typeArgs = {Type.GetType("Person")};
Type deserialiserType = genericType.MakeGenericType(typeArgs);
object repository = Activator.CreateInstance(deserialiserType);
MethodInfo genericMethod = deserialiserType.GetMethod("FromFile");
genericMethod.Invoke(repository, new[] {file});
Thank you all for your help