首页 \ 问答 \ 动态加载DLL(Dynamic loading of DLL)

动态加载DLL(Dynamic loading of DLL)

我有一个需要使用大量插件的程序。

每个插件必须支持一个非常基本的接口,这个接口是在一个DLL中定义的(为简单起见,IBaseComponent)。

每个插件都将位于特定目录(AppDirectory \ plugin \ plugin-type)中。 每个插件都可以具有该插件的dll(AppDirectory \ plugin \ plugin-type \ plugin-name.dll)的任何名称。

所以我需要检查每个插件子目录,找到每个拥有支持IBaseComponent接口的类的插件,实例化类并调用插件中的某些函数。

好吧,一切都很好,这些都不是特别困难。 但问题是我似乎遇到了一些奇怪的问题。

每个插件需要在单独的插件文件夹中有Base.dll文件(而不仅仅是在将要加载插件的程序中),并且似乎我在动态加载dll时遇到了很多错误和警告,这些dll也有需要加载。

我在用着:

pluginModule = System.Reflection.Assembly.ReflectionOnlyLoadFrom(PathToAssembly);

为了抓住插件DLL并使用:

types = moduleAssembly.GetTypes();

以便获取dll中包含的类型。 我遍历这些类型并检查单个类型是否为IBaseComponent接口(表示这是对加载类有效的):

if (type.GetInterface("FrameworkNameSpace.IBaseComponent") != null)
    //it's of the IBaseComponent interface

后来,为了从dll中实际创建一个类的实例,我使用:

pluginModule = System.Reflection.Assembly.LoadFrom(PathToAssembly);

然后使用:

types = component.GetTypes();

为了获得模块中的类型,然后选择并加载支持与上述相同的接口的类。

这个问题似乎在我使用时出现:

types = component.GetTypes();

实际上试图加载类时,而不是简单地看它。 (因此,我对LoadFromReflectionOnlyLoad的不同用法)

我在GetTypes调用(第二次插入,但从来没有第一次!)收到的异常是:

{"Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."}

LoaderExceptions属性如下所示:

{"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null}

我不确定为什么会发生这种情况。 该DLL位于插件文件夹中,并且包含IBaseComponent接口的DLL也位于每个插件目录中。 我是否以这种错误的方式去做?

我还需要在每个plugin子目录中保存一个包含IBaseComponent的DLL的副本,以及程序本身使用的DLL的副本,还是我做了一些不正确的事情,这会让我去除这个需求?

我知道MEF是我想用的,但不幸的是,因为我需要在.net 2.0上支持这个功能,所以我无法使用MEF。


I have a program which needs to use a large number of plugins.

Each plugin must support a very basic interface, this interface is defined in a DLL (IBaseComponent for simplicity of question sake).

Each plugin will be in a specific directory (AppDirectory\plugin\plugin-type). Each plugin can have any name for the plugin's dll (AppDirectory\plugin\plugin-type\plugin-name.dll).

So I need to check through each plugin subdirectory, find each plugin that has a class which supports the IBaseComponent interface, instantiate the class and call some functions on the plug in.

ok, all fine and dandy, none of this is particularly hard. The problem though is that I seem to be running into some weird issues.

Every plugin needs to have the Base.dll file in the individual plugin folders (instead of just in the program that will be loading the plugin) and also it seems that I get many errors and warnings around dynamically loading a dll which have dll's that also need to be loaded.

I'm using:

pluginModule = System.Reflection.Assembly.ReflectionOnlyLoadFrom(PathToAssembly);

in order to grab the plugin dll and using:

types = moduleAssembly.GetTypes();

in order to grab the types contained within the dll. I am iterating over the types and checking if the individual type is of the IBaseComponent interface (signifying this is a valid to load class) with:

if (type.GetInterface("FrameworkNameSpace.IBaseComponent") != null)
    //it's of the IBaseComponent interface

Later, in order to actually create an instance of the class from the dll I use:

pluginModule = System.Reflection.Assembly.LoadFrom(PathToAssembly);

and then use:

types = component.GetTypes();

In order to get the types within the module then select and load the class which supports the interface same as above.

The problem seems to be on when I use:

types = component.GetTypes();

When actually trying to load the class, Instead of simply looking at it. (Hence my different usage of LoadFrom and ReflectionOnlyLoad)

The Exception I receive on the GetTypes call (on a second plug in, but never the first!) is:

{"Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."}

With the LoaderExceptions property as:

{"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null}

I'm unsure why this occurs. The DLL is in the plugin folder, and the DLL containing the IBaseComponent interface is also in every one of the plugin directories. Am I going about this the wrong way?

Also is it required for me to keep a copy of the DLL containing IBaseComponent within each plugin subdirectory as well as the one used by the program itself, or am I doing something incorrectly that would allow me to remove this requirement?

I am aware of MEF which is what I wanted to use but unfortunately because I am required to support this on .net 2.0 I am unable to use MEF.


原文:https://stackoverflow.com/questions/5123170
更新时间:2023-04-26 06:04

最满意答案

有反编译.class文件的工具,例如JAD http://en.wikipedia.org/wiki/JAD_(JAva_Decompiler

从这个意义上讲,如果您要分发类文件,我不会说java是安全的。 但是,您可以采取一些措施,例如使用ProGuard等混淆器(http://proguard.sourceforge.net/)


There are tools to decompile .class files, such as JAD http://en.wikipedia.org/wiki/JAD_(JAva_Decompiler)

In that sense, I wouldn't say java is secure if you're distributing your class files. However, there are measures you can take such as using an obfuscator such as ProGuard (http://proguard.sourceforge.net/)

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。