Question

I am trying to serialize a couple of nested classes to and from an XML file.

My load and save methods use XmlSerializer/TextWriter/TextReader. This works fine if I don't use Dotfuscator. But if I use Dotfuscator, it fails to write the classes to the file and I only get the root XML tags.

I have since tried explicitly naming each field like so:

[XmlRoot("ParentClass")]
public class ParentClass
{
    [XmlArray("ChildClasses")]
    public List<ChildClass> ChildClasses;
}

[XmlType("ChildClass")]
public class ChildClass
{
    [XmlElement("Property")]
    public string Property;
}

Basically, if it's getting serialized, I've given it explicit naming. However I tested this and it still doesn't work with the Dotfuscator. Anyone know how to get it to work?

Était-ce utile?

La solution

XML Serialization uses reflection, so the fact that Dotfuscator can rename these classes is probably causing an issue.

Try this:

[Obfuscation(Feature = "renaming", Exclude = true)]
public class ParentClass
{
   ...

Decorate each class that will be XML Serialized with this decorator.

Autres conseils

If you don't mind not obfuscating those types, add an exclude attribute:

[Obfuscate(Exclude=true)]
[XmlRoot("ParentClass")]  
public class ParentClass  
{  
    [XmlArray("ChildClasses")]  
    public List<ChildClass> ChildClasses;  
}  

[Obfuscate(Exclude=true)]    
[XmlType("ChildClass")]  
public class ChildClass  
{  
    [XmlElement("Property")]  
    public string Property;  
}  

Or add the [Serializable] attribute to the classes you don't want renamed.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top