0

I am not sure if this can be done but I am wondering if it is possible to specify the 'T' type of a generic at run time. I have an object that has been serialized and stored in the database. The type of the object has also been stored. So when the object is retrieved I have the serialized object stored in a string variable as it's type also stored in a string variable. What I want to be able to do is something like this:

var serializer = new AbcSerializer();
var typeString = "Abc.Group.Common.Domain.DomainObject, Abc.Group.Common.Domain";
var serializedObject = "some serialized data here...";
var retObj = serializer.Deserialize<Type.GetType(typeString)>(serializedObject);

Does someone know if this is possible?

Other questions that are similar to this one talk about using MakeGenericType like this:

Type constructed = d1.MakeGenericType(typeArgs);

And then doing:

object o = Activator.CreateInstance(constructed);

to create an instance of that object. That is not what I am trying to do. I want to create an instance of my object from data that has been serialized.

Greg P
  • 107
  • 1
  • 9
  • Creating an object instance and then filling in the serialized data sounds legit to me... how is this not what you are trying to do? Or if you actually have `serializer.Deserialize(...)` at hand, you may want to dynamically invoke `Deserialize` with a dynamically inferred type parameter. – grek40 Sep 03 '16 at 12:52
  • See [MakeGenericMethod](https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod(v=vs.110).aspx) for the method equivalent to `MakeGenericType` – grek40 Sep 03 '16 at 12:56

0 Answers0