vendredi 3 juin 2016

.NET get class name from String

I need to use this method :

public T DeserializeFromXmlString<T>(string xmlString)
{
    var serializer = new XmlSerializer(typeof(T));
    using (TextReader reader = new StringReader(xmlString))
    {
        return (T)serializer.Deserialize(reader);
    }
}

And to use it, I need to know the generic type T. Imagine that I have a class "Apple". I will use it like this:

var myapple= DeserializeFromXmlString<Apple>(someXmlString);

But I need to be able to remove this <Apple> and replacing it by something else considering that I have the string "Apple".
The goal is to convert a string to be able to use it in this method as a generic type T.





Aucun commentaire:

Enregistrer un commentaire