문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top