在运行时加载托管dll并调用方法

编程入门 行业动态 更新时间:2024-10-27 18:26:10
本文介绍了在运行时加载托管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并调用方法

本文发布于:2023-11-09 20:11:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   方法   dll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!