简体   繁体   中英

Postsharp 3rd party components events interception

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.

A workaround is to do it the other way around: Use an aspect on the method that is hooked to the event and cancel the normal execution of the method if the condition is met. This does not prevent the event form being raised but it prevents your event handling code from being executed.

[EventInter]
private void someForm_FormClosed(object sender, EventArg arg) {}

We use this approach a lot in our project. We have several aspects that apply to event handling methods (exception handling, cursors handling, etc...).

We go a little further, we apply the aspects at the assembly level and we use CompileTimeValide to recognize the signature of an event handling method. In theory, it's not 100% reliable, but we have not found any problems with this approach so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM