简体   繁体   中英

using System.Reflection

Let us consider im having an application named as AAA. Now Im Loading an assembly named as BBB.The aseembly BBB is having the declaration of event and delegate. The handler to the event in BBB is available in AAA. while loading the assembly BBB, i need to add the handler for the event in AAA. Whenever the event occurs in BBB, the handler method in AAA shoud gets executed automatically. How to accomplish this..........?

i coded like dis in AAA

Assembly tstComponent = Assembly.LoadFile(BBB);
            Type Global = tstComponent.GetType(ClassInBBB, false, true);
if (Global != null)
            {

                EventInfo l_objevent = Global.GetEvent("OnGetdelInBBB");
                Type l_objEveType = l_objevent.EventHandlerType;
                Type Dis = Assembly.GetExecutingAssembly().GetType("AAA", false, true);
                MethodInfo l_method = Dis.GetMethod("HandlerinAAA");
                Delegate d = Delegate.CreateDelegate(l_objEveType, l_method);//Getting argument bind exception in this line
                MethodInfo addHandler = l_objevent.GetAddMethod();
                Object[] addHandlerArgs = { d };
                addHandler.Invoke(Dis, addHandlerArgs);

             }   

please help me. Thanks in advance

See How to: Hook Up a Delegate Using Reflection

In your code sample your state an exception - that can happen for a number of reasons , but my gut says it is because the method signatures are not correct. You may want to also try a different overload which accepts the object as the second parameter.

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