是否有人知道如何以編程方式將[XmlIgnore]
屬性添加到c#中的類屬性?編程地在c#中爲序列化添加[XmlIgnore]屬性
我想這樣做只有一個類有一個或兩個字段序列化,因爲我需要在運行時。
非常感謝提前。
是否有人知道如何以編程方式將[XmlIgnore]
屬性添加到c#中的類屬性?編程地在c#中爲序列化添加[XmlIgnore]屬性
我想這樣做只有一個類有一個或兩個字段序列化,因爲我需要在運行時。
非常感謝提前。
通過將XmlAttributeOverrides對象傳遞給XmlSerializer構造函數,可以動態重寫XML序列化屬性。
XmlAttributes samplePropertyAttributes = new XmlAttributes();
samplePropertyAttributes.XmlIgnore = true;
XmlAttributeOverrides sampleClassAttributes = new XmlAttributeOverrides();
sampleClassAttributes.Add(typeof(SampleClass), "SampleProperty", samplePropertyAttributes);
var serializer = new XmlSerialized(typeof(SampleClass), sampleClassAttributes);
是啊!實施'n'測試:它的工作! – user3244672
謝謝隊友! ;))) – user3244672
另請注意,XmlSerializer(Type,XmlAttributeOverrides)構造函數在運行時動態創建程序集,並且不會將其緩存!所以每次調用這個構造函數都會創建另一個可能導致內存泄漏的程序集。 XmlSerializer是線程安全的,所以如果你打算使用這個構造函數,最好緩存你創建的XmlSerializer – Sal
我剛讀過它,但是,不,它不是我的情況。不管怎樣,謝謝你。 – user3244672
這不是重複的。 –