How to User Reflection to pass a type T in Generics RRS feed

  • Question

  • Hi,

    I want to deserialise a json string into my PhotoExhibitCollectionDto object and ordinarily I would do:

    var collection = Com.BathSpaCentral.Utilities.Json.Deserialize<PhotoExhibitCollectionDto>(jsonString);

    However I am in a circumstance where I only have a derived string "PhotoExhibitCollectionDto" (from "PhotoExibit" + CollectionDto)

    How can I use reflection to let the T in the following generics case that it is actually a PhotoExhibitCollectionDto class?

    var collection = Com.BathSpaCentral.Utilities.Json.Deserialize<T>(jsonString);

    I imagine something clever like:

    // clever reflection code, then

    var collection = Com.BathSpaCentral.Utilities.Json.Deserialize<CleverReflectionObjectHere>(jsonString);

    Any hints on how I can do this?

    What is behind it is that in one circumstance I might be deserialising to a PhotoExhibitCollectionDto, yet on another occasion to a MusicExhibitCollectionDto, and in each case I would only have derived the string "PhotoExhibitCollectionDto" or the string "MusicExhibitCollectionDto".


    Thursday, December 12, 2013 1:03 PM


  • So you want a intelligent "T"  which can deserialize both the class type i.e. PhotoExhibitCollectionDto & MusicExhibitCollectionDto? If yes then you should a create a combined class with members of both the class.
    Friday, December 13, 2013 10:34 AM