使用的System.Reflection [英] using System.Reflection

查看:92
本文介绍了使用的System.Reflection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看看其命名为AAA的应用程序IM。
现在林加载命名为BBB.The aseembly BBB是有事件和委托的声明组装。
的处理程序在BBB事件是AAA可用。
加载装配BBB,我需要添加处理程序为AAA事件。
每当事件发生BBB,在AAA 768,16处理程序方法被自动执行。
如何做到这一点..........?

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..........?

我编码方式类似于存款保险的AAA

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);

             }   

请帮助我。在此先感谢

推荐答案

请参阅的如何:挂钩一个委托使用反射

在您的代码示例的状态异常 - 即可能发生的原因有的,但我的直觉说,这是因为该方法签名是不正确的。你可能想也尝试它接受对象的不同过载第二个参数。

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.

这篇关于使用的System.Reflection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆