| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot apply the aspect <Aspect> to a property or an event of the type <DeclaringType>: property-level and event-level aspects can be applied only to the declarations of the current assembly. |
Cause
PostSharp modifies only the assembly that it is building. An aspect on a property or an event changes the accessors of this property or event, so its target must be declared in the current assembly.
An aspect that implements IAspectProvider returns AspectInstance objects, each with a reflection object as its target. When the target is a PropertyInfo or an EventInfo, PostSharp looks for its declaring type among the types of the current assembly, and reports LA0031 when it cannot find it. <Aspect> is the provided aspect, and <DeclaringType> the type that declares the property or the event. PostSharp reports the message on the aspect provider.
In the current version of PostSharp, a property or an event of a type defined in a referenced assembly, such as a library of your solution or a class of .NET, does not reach this check: the build fails later with an unhandled exception (PS0099), for example ModuleMismatchException: Module mismatch. for a LocationInterceptionAspect. This is why no example can show this message.
Resolution
- In the ProvideAspects method, provide aspects only to the properties and events of types defined in the current assembly, for example by comparing Assembly of the declaring type with the assembly that the provider processes.
- To add aspects to the types of a library that you own, add PostSharp to the project of the library and apply the aspects there.
- To change the behavior of a type that you do not own, wrap it in a type of your assembly, and apply the aspect to the properties of the wrapper.
See Also
Reference
IAspectProvider
AspectInstance
Other Resources