How to figure out the type when deserializing?
If I have 10 different objects serialized/deserialized using a Generic T class that uses the XmlSerializer internally, how can I know which type to use when deseria开发者_开发知识库lizing it?
MyXmlSerializer.Deserialize<MyObject> ( ... )
How can I know MyObject? All I have is a stringstream.
You could implement a static method to try to deserialize each possible type. This way you wouldn't have to break your object model, to support run-time type discovery.
You would still have to have some guesses for the types stored in the XML file. The simle XML file serialization does not store detailed type information by default. It seems like there should be an attribute that forces the serializer to store the detailed type name, but I couldn't find it...
public static bool TryDeserialize<T>(XmlReader reader, out T obj) where T : class
{
// Return null, if we can't deserialize
obj = null;
XmlSerializer deserializer = new XmlSerializer(typeof(T));
bool result = deserializer.CanDeserialize(reader);
if (result)
{
// Get the object, if it's possible
obj = deserializer.Deserialize(reader) as T;
}
return result;
}
You can use known types:
var ttt = new []{typeof(Tst1), typeof(Tst2)};
var ser = new XmlSerializer(typeof(object), ttt);
// STEP 1
ser.Serialize(File.CreateText("D:\\test.xml"), new Tst2());
// STEP 2
var res = ser.Deserialize(File.OpenText("D:\\test.xml"));
Console.WriteLine(res);
精彩评论