Reputation: 776
I'm using the XmlSerializer to de-serialize a response stream from a web request. This works fine but I want to break this code out so that it's reusable throughout my project.
My aim is to accept XML responses back and convert them to a model object. As an example of what I've had working in my proof of concept:
var serializer = new XmlSerializer(typeof(MyTestModel));
I had hoped I could replace the contents of 'typeof' with a variable. That way I could pass in the model name via the constructor. However this approach doesn't work.
I did look at generics:
var serializer = new XmlSerializer(typeof(T));
But as I'm programming to an interface I can't declare any methods in the interface as having a type of 'T'.
So I thought rather than spend hours and hours I would reach out to those in the know to get some guidance on how best to code this piece.
Thanks in advance!
Upvotes: 0
Views: 439
Reputation: 33738
Making comment an answer so the question can be closed.
Type type = typeof(MyTestModel); ...new XmlSerializer(type);
Upvotes: 2