| Severity | Error |
| Reported by | PostSharp |
| Message | The type "<SerializerType>" cannot be used as the serializer of the aspect "<Aspect>" because it is not derived from the PostSharp.Aspects.Serialization.AspectSerializer class. |
Cause
PostSharp serializes the aspect instances at build time and deserializes them at run time, as Understanding Aspect Serialization describes. By default, it chooses the serializer from the aspect class: PortableAspectSerializer for a class annotated with PSerializableAttribute. You can choose another serializer with the SerializerType property of an aspect configuration attribute, such as SerializerType or the one of OnMethodBoundaryAspectConfigurationAttribute, or with the SerializerType property of the aspect.
PostSharp creates an instance of this type and calls its methods, so the type must be derived from AspectSerializer. PostSharp reports LA0021 when it is not, for example when the serializer class has Serialize and Deserialize methods but no base class. <SerializerType> is the configured type, and <Aspect> the aspect class.
PostSharp does not stop after reporting LA0021. When the type has a public parameterless constructor, the build then fails with an unhandled exception (PS0099) because PostSharp cannot convert the instance to AspectSerializer; otherwise, it also reports LA0022 and LA0001. This is why no example can show this message alone.
Resolution
- Derive the serializer from AspectSerializer, override its
SerializeandDeserializemethods, and give it a public parameterless constructor. - Unless the aspect needs a serializer of its own, remove the
SerializerTypeproperty and annotate the aspect class with PSerializableAttribute. - To build the aspect at run time instead of serializing it, set
SerializerTypeto MsilAspectSerializer.
See Also
Reference
AspectSerializer
AspectConfigurationAttribute
MsilAspectSerializer
Other Resources