Reputation: 53
I am new to AOP and PostSharp. I am trying to apply an aspect to a predefined class, for example the System.Windows.Forms.Button class and intercept some of its methods. Can you guide me how to do that with PostSharp? Can we use PostSharp to capture event raised from a given type of UIElement?
Thanks a lot!
Upvotes: 1
Views: 126
Reputation: 1850
MethodInterceptionAspect works on methods declared in external DLL's by intercepting the call on call site. That means that the method itself is not transformed and instead the place where you call the method is changed. The original assembly is not transformed.
You can do this by multicasting the attribute on your assembly and explicitly specifying AttributeTargetAssemblies
:
[assembly:MyAspect(AttributeTargetAssemblies="myassembly,...", AttributeTargetTypes="...", AttributeTargetMembers="..."]
This implies few limitations. For example if the code is called from external code, the call will not get intercepted. In your case you could use this to intercept a method that is always called from the designer code (like this.Controls.Add(...)
).
In case of buttons in your source code I think there will be a different viable approach. If you create LocationInterceptionAspect
and multicast it on all fields of type Button
, you will be able to intercept creation of all buttons in your application. In the aspect code (set handler), you can then register to the Clicked
event of those buttons.
Upvotes: 1
Reputation: 2497
I doubt thats possible since i dont think ilweaving step of postsharp can manipulate already compiled external dlls.
What you can do is implement your own button (extend from button) and have those methods intercepted
Upvotes: 0