I have a question about events interception with c# and Postsharp.
I would like to cancel the execution of events like BeforeDropDown, RowSelected MouseClick with EventInterceptionAspect in postsharp.
But i can not find a proper place where i can write the code. example:
i tried something like this:
[Serializable]
class EventInter : EventInterceptionAspect
{
public override bool CompileTimeValidate(System.Reflection.EventInfo targetEvent)
{
return "FormClosed".Equals(targetEvent.Name);
}
public override void OnInvokeHandler(EventInterceptionArgs args)
{
if condition executes method otherwise no
}
}
in the form:
[EventInter]
public partial class Frm_RomperMesa : KryptonForm
But it didn´t work. So i want to know if it is possible to achieve what i want.
Thanks in advace. I hope be clear.
yes, it is possible. The problem is, you're trying to apply an event interception aspect to an event defined in another assembly which you can't do within your code. You can't even override the event because it's setup to be handled using the base Form type in the designer code behind
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
you will have to modify the assembly to do this. Use the following aspect and the links to modify your
public class EventAspectProvider : TypeLevelAspect , IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type t = (Type)targetElement;
EventInfo e = t.GetEvents().First(c => c.Name.Equals("FormClosing"));
return new List<AspectInstance>() { new AspectInstance(e, new EventInter()) };
}
}
[Serializable]
public class EventInter : EventInterceptionAspect
{
public override void OnInvokeHandler(EventInterceptionArgs args)
{
int x = 0;
if (x > 0) //Do you logic here
{
args.ProceedInvokeHandler();
}
}
}
Basically it boils down to modifying the System.Windows.Forms.dll which I don't recommend. But if it's some other 3rd party vendor library, then go for it.