如何将dll文件附加到使用“反射”的项目中在c#? [英] How to append a dll file to my project that use "reflection" in c#?

查看:127
本文介绍了如何将dll文件附加到使用“反射”的项目中在c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#和dll插件中编写程序。

我想在form_load方法中编写代码来搜索程序目录中的dll文件。

然后附加这个dll文件并运行它们。

我知道这个动作可以通过system.reflection和system.io来完成,但我不知道怎么做!

Hi, I write a program in c# and a dll plugin.
I want to write code in the form_load method that search dll files in program directory.
Then append this dll files and run these.
I know that this actions can be done with "system.reflection" and "system.io", but i dont know how do it!

推荐答案

附加DLL只不过是荒谬的。您始终可以使用反射加载程序集(程序集,而不是DLL):

https://msdn.microsoft.com/en-us/library/system.reflection.assembly%28v=vs.110%29.aspx [< a href =https://msdn.microsoft.com/en-us/library/system.reflection.assembly%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ],

https:// msdn.microsoft.com/en-us/library/1009fa28(v=vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/ky3942xh(v=vs.110).aspx [ ^ ] ...查看所有其他 Assembly.Load * 方法。



然后你可以使用 Assembly.LoadTypes 或其他类似方法来获取类型:https://msdn.microsoft.com/en-us/library /system.reflection.assembly.gettypes(v=vs.110).aspx [ ^ ]。



随着类型,你可以得到你需要的成员,尤其是构造者;然后你可以使用 System.Reflection.ConstructorInfo 实例化任何类型:

https://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo%28v=vs.110% 29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo.invoke%28v=vs.110%29.aspx [ ^ ]。



然后做任何你想做的事。



这将是最好通过使用一些声明来获得所需的类型主机和已加载的程序集。最好的选择之一是通过它应该实现的接口找到类型。有关详细信息,请参阅我过去的答案:

通过它的字符串表示从集合中收集类型 [ ^ ],

访问驻留在插件dll中的自定义对象 [ ^ ]。



请注意,在运行时加载的程序集不是项目的一部分,否则动态执行它根本就没有任何意义;你可以简单地引用组件并以通常的方式使用它。动态地,您使用程序集main 模块的文件名(具有.NET程序集清单的文件名)或通过其强名称从GAC加载程序集。



-SA
"Append a DLL" is nothing but absurd. You can always load an assembly (assembly, not "DLL") using Reflection:
https://msdn.microsoft.com/en-us/library/system.reflection.assembly%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/1009fa28(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/ky3942xh(v=vs.110).aspx[^]… see all other Assembly.Load* methods.

Then you can use Assembly.LoadTypes or other similar method to get types: https://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes(v=vs.110).aspx[^].

With the type, you can get the members you need and, in particular, constructors; then you can instantiate any type using System.Reflection.ConstructorInfo:
https://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo.invoke%28v=vs.110%29.aspx[^].

And then do whatever you want.

It would be the best to base getting the the type you need by using some declarations used by both host and loaded assemblies. One of the best option is to find the type by the interface it is supposed to implement. For further detail, please see my past answers:
Gathering types from assemblies by it's string representation[^],
Access a custom object that resides in plug in dll[^].

Note that the assembly loaded during runtime is not the part of your project, otherwise doing it dynamically would not make any sense at all; you could simply reference the assembly and use it in a usual way. Dynamically, you load assembly either using the file name of the assemblies main module (the one with .NET assembly manifest) or from GAC by its strong name.

—SA


这篇关于如何将dll文件附加到使用“反射”的项目中在c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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