本文介绍了在运行时加载托管dll并调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何调用dll中定义的类的方法. dll中定义的类使用以下接口:
I would like to know, how I can call the methods of the class defined in my dll. The class defined in the dll uses this interface:
interface class ILightControl{ void Settings(); };这是我需要调用该方法的部分.
This is the part, where I need to call the method.
System::Reflection::Assembly^ x = System::Reflection::Assembly::LoadFile("C:\\LightControl.dll"); array<Type^>^t = x->GetTypes(); Type^InterfaceType; Object^obj; for(int i = 0; i < t->Length; i++){ InterfaceType = t[i]->GetInterface("ILightControl"); if(InterfaceType){ obj = Activator::CreateInstance(t[i]); //I need to call Settings(); here } }现在如何调用该方法?非常感谢您的帮助!
How call I now call the method? Thank you very much for your help!
推荐答案在这种情况下,我认为您可能必须使用反射调用该方法 In that case I think you may have to call the method using reflection System::Reflection::MethodInfo^ mi = interfaceType->GetMethod("Settings"); cli::array<System::Object> parameters = gcnew cli::array<System::Object>(); // add any parameter here System::Object^ retVal = mi->Invoke(obj, parameters)
((ILightControl)obj)->Settings();
尝试一下作为测试 Try this as a test <br /> <br /> LightControl^ lc = (LightControl^)Activator::CreateInstance(LightControl::typeid));<br /> ((ILightControl^)lc)->Settings(); <br />
更多推荐
在运行时加载托管dll并调用方法
发布评论