C# 如何从 /bin 目录中加载所有程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1288288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:43:08  来源:igfitidea点击:

how to load all assemblies from within your /bin directory

c#assemblies

提问by mrblah

In a web application, I want to load all assemblies in the /bin directory.

在 Web 应用程序中,我想加载 /bin 目录中的所有程序集。

Since this can be installed anywhere in the file system, I can't gaurantee a specific path where it is stored.

由于它可以安装在文件系统中的任何位置,因此我无法保证存储它的特定路径。

I want a List<> of Assembly assembly objects.

我想要一个 List<> 的 Assembly 程序集对象。

采纳答案by Wolfwyrd

Well, you can hack this together yourself with the following methods, initially use something like:

好吧,您可以使用以下方法自己破解它,最初使用类似的方法:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

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:

获取当前程序集的路径。接下来,使用带有合适过滤器的 Directory.GetFiles 方法遍历路径中的所有 DLL。您的最终代码应如下所示:

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));

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)

请注意,我尚未对此进行测试,因此您可能需要检查 dll 是否实际包含完整路径(如果不包含则连接路径)

回答by Patrik Svensson

You can do it like this, but you should probably not load everything into the current appdomain like this, since assemblies might contain harmful code.

您可以这样做,但您可能不应该像这样将所有内容加载到当前应用程序域中,因为程序集可能包含有害代码。

public IEnumerable<Assembly> LoadAssemblies()
{
    DirectoryInfo directory = new DirectoryInfo(@"c:\mybinfolder");
    FileInfo[] files = directory.GetFiles("*.dll", SearchOption.TopDirectoryOnly);

    foreach (FileInfo file in files)
    {
        // Load the file into the application domain.
        AssemblyName assemblyName = AssemblyName.GetAssemblyName(file.FullName);
        Assembly assembly = AppDomain.CurrentDomain.Load(assemblyName);
        yield return assembly;
    } 

    yield break;
}

EDIT: I have not tested the code (no access to Visual Studio at this computer), but I hope that you get the idea.

编辑:我还没有测试代码(在这台计算机上无法访问 Visual Studio),但我希望你能明白。

回答by Jason S

To get the bin directory, string path = Assembly.GetExecutingAssembly().Location;does NOTalways work (especially when the executing assembly has been placed in an ASP.NET temporary directory).

为了得到bin目录,string path = Assembly.GetExecutingAssembly().Location;不要总是工作(特别是在执行程序集已被放置在一个ASP.NET临时目录)。

Instead, you should use string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");

相反,你应该使用 string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");

Further, you should probably take the FileLoadException and BadImageFormatException into consideration.

此外,您可能应该考虑 FileLoadException 和 BadImageFormatException。

Here is my working function:

这是我的工作功能:

public static void LoadAllBinDirectoryAssemblies()
{
    string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin"); // note: don't use CurrentEntryAssembly or anything like that.

    foreach (string dll in Directory.GetFiles(binPath, "*.dll", SearchOption.AllDirectories))
    {
    try
    {                    
        Assembly loadedAssembly = Assembly.LoadFile(dll);
    }
    catch (FileLoadException loadEx)
    { } // The Assembly has already been loaded.
    catch (BadImageFormatException imgEx)
    { } // If a BadImageFormatException exception is thrown, the file is not an assembly.

    } // foreach dll
}

回答by xspydr

I know this is a old question but...

我知道这是一个老问题,但是......

System.AppDomain.CurrentDomain.GetAssemblies()

System.AppDomain.CurrentDomain.GetAssemblies()