序列化如何动态忽略特定类型的所有成员

时间:2010-09-15 14:53:16

标签: c# .net serialization xml-serialization xmlignore

我希望在序列化时忽略Dictionary的所有元素,因为这些成员会导致异常

示例类:

public class TestClass{
    public string StringTest { get; set; }
    public int IntTest { get; set; }
    public Dictionary<int, string> someDictionary { get; set; }
}

我尝试了什么(不成功)

XmlAttributeOverrides xOver = new XmlAttributeOverrides();

XmlAttributes attrs = new XmlAttributes();
attrs.XmlIgnore = true;

xOver.Add(typeof(Dictionary<int, string>), attrs);

XmlSerializer serialiser = new XmlSerializer(objectToConvert.GetType(), xOver);
serialiser.Serialize(writer, objectToConvert);

1 个答案:

答案 0 :(得分:0)

您是否尝试过这样做。

[XmlIgnore]
public Dictionary<int.string> someDictionary{get;set;}