Search code examples
c#xml-serializationdatacontract

Determine DataContract type from XElement


I am using the System.Runtime.Serialization namespace outside of WCF and would like to find out what Type an element represents - is there a name resolver? I'd really like something along the lines of:

Type ResolveName(XmlQualifiedName typeName);

I see that there's something similar in the internal workings of WCF, but I can't seem to find a public one.

Thanks!


Solution

  • Part of the point of contract-based serialization ala DataContractSerialization is that it isn't tied to specific types (note: NetDataContractSerializer doesn't share this). As such, there could be zero, one, or many candidate types. Which would it choose?

    So no; AFAIK that is fundamentally not possible. You could perhaps use reflection over all types in all loaded assemblies, looking for [DataContract] and apply he tests manually, of course.

    Normally you have existing Type information (either the root type or the current type), so you only have a small set of candidate types (KnownType etc) to consider.