Reputation: 23207
I've created a simple aspect:
[Serializable()]
public class NullableCallAspect : PostSharp.Aspects.OnMethodBoundaryAspect
{
public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args)
{
if (args.Instance == null)
args.FlowBehavior = PostSharp.Aspects.FlowBehavior.Return;
}
}
Essentially, I want that whichever instance.method
call that instance == null
it doesn't enter in the method. I'm figuring out, I need to change the inheritance of the aspect. So, I would need to change OnMethodBoundaryAspect
by another one. This would be the first question.
The other question is around how to apply this aspect to methods calls from classes that inherits of an interface of another assembly.
I've tried this, but it doesn't quite work:
[assembly: UI.Aspects.NullableCallAspect(
AttributeTargetAssemblies = "UIAppearanceExtensibility",
AttributeTargetTypes = "UI.Appearance.Extensibility.*.I*AppearanceManager",
AttributeTargetMembers = "handle*"
)]
Upvotes: 0
Views: 240
Reputation: 4072
This kind of aspects would require call site interception which is not supported by PostSharp. Both OnMethodBoundaryAspect and MethodInterceptionAspect modify target method not call site itself - instance is still required when calling method decorated by these aspects.
EDIT: There is a hack how to force PostSharp to intercept call sites. It is when an aspect is multicasted to types in different assembly. If all methods are implemented in project ClassLibrary1 and they are called just from MyApplication project then the aspect can be multicasted in MyApplication project like this:
[assembly:
NullableCallAspect(AttributeTargetAssemblies = "ClassLibrary1", AttributeTargetTypes = "ClassLibrary1.*",
AttributeTargetMembers = "handle*", AttributeTargetElements = MulticastTargets.Method)]
If there is a convention that all implementation of IAppearanceManager end has name with suffix AppearanceManager then the multicast needs to be changed:
[assembly:
NullableCallAspect(AttributeTargetAssemblies = "ClassLibrary1", AttributeTargetTypes = "ClassLibrary1.*AppearanceManager",
AttributeTargetMembers = "handle*", AttributeTargetElements = MulticastTargets.Method)]
If there is no such convention then a IAspectProvider can be used for the multicasting.
This is not usable when there are calls to methods decorated by NullableCallAspect within same assembly - call sites are not intercepted in such case.
Upvotes: 1