在运行时加载托管dll并调用方法 [英] Loading managed dll at runtime and calling a method
本文介绍了在运行时加载托管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并调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文