When you have a large object model, annotating it for the WCF serializer (
can be tedious. All classes and derived classes need to be annotated with the
attribute, and all properties need the
[DataMember] custom attribute. If you forget a single
attribute, no build-time error or run-time will occur, but your program will behave incorrectly and the
defect may be difficult to diagnose.
In many cases, what you actually need is to serialize all classes derived from a common base class, and by default all properties should be serialized. This example demonstrates how to address this situation.
AutoDataContractAttribute aspect automatically adds custom attributes
to the target and derives classes so they can be serialized by the WCF formatter. By default, the aspect adds the
[DataMember] custom attribute to all properties. Properties that must not be serialized must be
annotated with the custom attribute
As a result of using
AutoDataContractAttribute, you are not forced to pollute your code with
[DataMember] attribute, and most importantly, you will never forget to
[DataMember] attribute again.
What is being demonstrated?
This example demonstrates the use of
AutoDataContractAttribute aspect does not represent any code transformation in itself. Instead, it
provides instances of the
CustomAttributeIntroductionAspect aspect through the
Inheritance of the
AutoDataContractAttribute aspect from the base class to derived classes is
implemented thanks to the
[MulticastAttributeUsage(Inheritance = MulticastInheritance.Strict)]