如何加载所有组件从/ bin目录中 [英] how to load all assemblies from within your /bin directory
问题描述
在Web应用程序中,我要加载所有程序中的/ bin目录。
In a web application, I want to load all assemblies in the /bin directory.
由于这可以在任何地方在文件系统被安装,我无法gaurantee在那里存储的特定路径。
Since this can be installed anywhere in the file system, I can't gaurantee a specific path where it is stored.
我想一个List<装配装配对象>
I want a List<> of Assembly assembly objects.
推荐答案
好了,你可以用下面的方法,破解起来这个自己,最初使用类似:
Well, you can hack this together yourself with the following methods, initially use something like:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
得到的路径当前装配。接着,遍历所有的DLL中使用Directory.GetFiles方法用合适的过滤器的路径。您的最终code应该是这样的:
to get the path to your current assembly. Next, iterate over all DLL's in the path using the Directory.GetFiles method with a suitable filter. Your final code should look like:
List<Assembly> allAssemblies = new List<Assembly>();
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
foreach (string dll in Directory.GetFiles(path, "*.dll"))
allAssemblies.Add(Assembly.LoadFile(dll));
请注意,我没有测试过这个,所以你可能需要检查该DLL实际包含完整路径(如果它不串联路径)
Please note that I haven't tested this so you may need to check that dll actually contains the full path (and concatenate path if it doesn't)
这篇关于如何加载所有组件从/ bin目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!