I have several classes that I serialize/deserialize, each with a number of properties, some of which I'd like dynamically use the "Xml.Serialization.XmlIgnore" attribute on. The idea being if I want to save specific property info, I manage it by setting/clearing a flag. Is that even possible?
+2
A:
Is it possible to dynamically enable/disable a .net attribute?
No. Attributes are baked at the assembly metadata at compile time. You will need to implement a custom serialization.
Can you simply imagine the consequences of being able to add/remove attributes at runtime? You could provoke disasters by removing for example the Serializable attribute from the String class or setting the ComVisible attribute to false
on this same class :-)
Darin Dimitrov
2010-09-23 18:42:09
+2
A:
Yes, this is possible by using the XmlAttributesOverrides class. It lets you generate the attributes dynamically rather than specifying them in your source code. The MSDN Library article for the class has a good example.
Hans Passant
2010-09-23 19:03:15