Background: This question is about logging the change tracking of a POCO class in C# .NET 4.0.
Let’s say I have a Person class with a Name (string) property. That Name property has a custom Attribute called [IsDirty(true/false)] that is set dynamically by a property-auditing class.
[IsDirty(true)]
public string Name { get; set; }
After the changes are detected and the attributes are set, I’m storing the object via normal XML Serialization in a MS SQL Database (XML column type).
What I can’t figure out is if it’s possible to somehow serialize my custom attribute IsDirty along with it’s current value – preferably as an XML attribute on the serialized XML element (Name) so that the final xml is like:
<Name IsDirty="true">John</Name>
Any ideas/info would be appreciated-
I think you’re going to have to write your own XML serialization for this and mix in some reflection to check attribute values on properties.
There’s a good guide to implementing the
IXMLSerializableinterface here. Unfortunately you will have to implement serialization of all properties in the class, but on the bright side, if you implementIXmlSerializablecorrectly, you can still use theXmlSerializerclass.In your serialization code, you can check the attribute value using something like this:
This code is untested and written from memory, so there are probably some bugs lurking around, but hopefully it’ll get you on the right track.