我需要一些帮助,将一个回调委托作为参数传递给一个动态链接的Dll方法,这样它最终可以调用它。 以下是我正在处理的代码的显着部分。它全部编译和所有其他动态链接的方法只传递常规争论(字符串,整数,bools,对象等)工作得很好。但是,当这部分代码运行并命中实际调用Dll Hello, I need some help getting a callback delegate passed as an argument to a dynamically linked Dll method so it in turn, can eventually call it.Below is the salient portions of code I''m dealing with. It all compiles andall other dynamically linked methods that only pass "regular" arguments (strings, ints, bools, objects, etc.) work great. However, when this portion of code runs and hits the line that acutally invokes the Dll 方法的行时, method,会引发以下异常: 对象类型无法转换为目标类型。 为什么我不允许传递回调委托:cbMyCallBack到 it throws the following exception: "Object type cannot be converted to target type." Why am I not allowed to pass the callback delegate: cbMyCallBack to the Dll Dll方法?当Dll callee 方法和参数类型匹配时,为什么要尝试转换参数? 任何帮助都会受到赞赏, TIA。 GloballyDelcared: public delegate void NotifyCB(); Inside Object1: public void SomeWorkerMethod( ) 对象2.DoWork(新NotifyCB(cbTheCallBack)) ... } public static void cbTheCallBack() // //这里回调...... } Inside Object2: privat e Assembly m_DllAssembly = Assembly.Load(DllName); private类型m_oTypeEngine = = m_DllAssembly.GetType(" Namespace.ObjectName"); 私有对象m_oActivator = Activator.CreateInstance(m_oTypeEngine); private类型m_TypeInterface = = m_oTypeEngine.GetInterface(" InterfaceName"); 私有MethodInfo m_miMyDllMethod = m_TypeInterface.GetMethod(" MyDllMethod");; ... 公共无效DoWork(NotifyCB cbMyCallBack) { ... 对象[] oArgs = {cbMyCallBack}; bool bRtn =(bool)m_miMyDllMethod.Invoke(m_oActivator,oArgs); public void MyDllMethod(< b / b) NotifyCB cbMyCallBack) { ... //做一些DLL工作 ... cbMyCallBack(); } - John C. Bowman 软件工程师 Thermo Electron Scientific Instruments Div。<在回复之前将其删除> jo*********@thermo.com method? Why is it trying to convert the argument at all when the Dll calleemethod and the argument type match? Any help would be appreciated, TIA. GloballyDelcared: public delegate void NotifyCB(); Inside Object1: public void SomeWorkerMethod() { ... Object2.DoWork(new NotifyCB(cbTheCallBack)) ... } public static void cbTheCallBack() { //Do the callback here ... } Inside Object2: private Assembly m_DllAssembly = Assembly.Load(DllName); private Type m_oTypeEngine = = m_DllAssembly.GetType("Namespace.ObjectName"); private object m_oActivator = Activator.CreateInstance(m_oTypeEngine); private Type m_TypeInterface = = m_oTypeEngine.GetInterface("InterfaceName"); private MethodInfo m_miMyDllMethod = m_TypeInterface.GetMethod("MyDllMethod");; ... public void DoWork(NotifyCB cbMyCallBack) { ... object[] oArgs = {cbMyCallBack}; bool bRtn = (bool)m_miMyDllMethod.Invoke(m_oActivator, oArgs); ... } Inside the Called DLL, Object3: public void MyDllMethod(NotifyCB cbMyCallBack) { ... //Do some DLL work ... cbMyCallBack(); } -- John C. Bowman Software Engineer Thermo Electron Scientific Instruments Div. <Remove this before reply> jo*********@thermo.com 这篇关于回调方法委托作为Dll函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 17:53