Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么没有在下拉列表中加载所有DLL_C#_Winforms - Fatal编程技术网

C# 为什么没有在下拉列表中加载所有DLL

C# 为什么没有在下拉列表中加载所有DLL,c#,winforms,C#,Winforms,我有一个这样的函数 public void SetOperationDropDown() { cmbOperations.DataSource = PluginManager.GetAllPlugins(); if(cmbOperations.Items.Count > 0) { cmbOperations.SelectedItem = cmbOperations.I

我有一个这样的函数

 public void SetOperationDropDown()
        {
            cmbOperations.DataSource = PluginManager.GetAllPlugins();

            if(cmbOperations.Items.Count > 0)
            {
                cmbOperations.SelectedItem = cmbOperations.Items[0];
            }
        }
此函数应获取所有DLL的名称

public class PluginManager
{

    /// <summary>
    /// This function gets the name of the plugins and return that in a List<string>.
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
public static List<string> GetAllPlugins()
{

    DirectoryInfo objDirectoryInfo = new DirectoryInfo("Plugins");
    FileInfo[] args = objDirectoryInfo.GetFiles("*.dll");

    List<string> assemblyNames = new List<string>();
    Assembly[] oAssemblies = new Assembly[args.Length];

    for(int assemblyCount = 0;assemblyCount < args.Length;assemblyCount++)
    {
        string executablePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

        oAssemblies[assemblyCount] = Assembly.LoadFile(executablePath.Remove(0,6) + "\\Plugins\\" + args[assemblyCount].Name);

        try
        {
            foreach(Type oType in oAssemblies[assemblyCount].GetTypes())
            {
                // Check whether class is inheriting from IMFDBAnalyserPlugin.
                if(oType.GetInterface("IMFDBAnalyserPlugin") == typeof(IMFDBAnalyserPlugin))
                {
                    assemblyNames.Add(args[assemblyCount].Name.Substring(args[assemblyCount].Name.LastIndexOf("\\") + 1));
                }
            }
        }
        catch(Exception ex)
        {
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
        }
    }

    // Passing data one application domain to another.
    AppDomain.CurrentDomain.SetData("AssemblyNames", assemblyNames.ToArray());
    return assemblyNames;
}
但并不是所有DLL都是通过这种方式创建的

 Type localType = assembly.GetType(fileName + "." + fileName);
我需要在下拉列表中加载DLL的完整列表


我哪里出了问题???你知道哪里出了问题吗?“args”变量是否包含任何内容?我怀疑你的问题在于你构建路径的方式。您应该使用Path类,而不是字符串操作

要获取当前可执行目录,请执行以下操作:

string executablePath = Path.GetDirectoryName(Application.ExecutablePath);
您也应该在GetAllPlugins()方法的开头使用它来构建DirectoryInfo。 要生成包含子文件夹的路径,请执行以下操作:

string pluginPath = Path.Combine(executablePath, "Plugin");
要获取插件子目录中具有dll扩展名的所有文件,请执行以下操作:

string[] pluginFiles = Directory.GetFiles(pluginPath, "*.dll");
请注意,返回的文件名包含整个路径。您不需要附加任何内容:

foreach (string fileName in pluginFiles)
{
    Assembly assembly = Assembly.LoadFrom(fileName);
    List<Type> types = assembly.GetTypes().ToList();
    foreach (Type type in types.FindAll(t => t.GetInterface("IMFDBAnalyserPlugin") != null)
    {
        ...
    }
}
foreach(pluginFiles中的字符串文件名)
{
Assembly=Assembly.LoadFrom(文件名);
列表类型=assembly.GetTypes().ToList();
foreach(types.FindAll中的Type(t=>t.GetInterface(“IMFDBAnalyserPlugin”)!=null)
{
...
}
}
应该有用

另外,不要使用子字符串,如果您想要文件名,有或没有扩展名,您应该使用Path.GetFileName或Path.GetFileNameWithoutExtension

并使用此方法实例化您的插件:

通过异常处理程序记录了哪些事件?
foreach (string fileName in pluginFiles)
{
    Assembly assembly = Assembly.LoadFrom(fileName);
    List<Type> types = assembly.GetTypes().ToList();
    foreach (Type type in types.FindAll(t => t.GetInterface("IMFDBAnalyserPlugin") != null)
    {
        ...
    }
}