Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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
从dll[c#]调用方法_C#_Dll_Methods_Call_Invoke - Fatal编程技术网

从dll[c#]调用方法

从dll[c#]调用方法,c#,dll,methods,call,invoke,C#,Dll,Methods,Call,Invoke,请告诉我如何从dll调用方法,您甚至可以使用常用的原语MessageBox.Show(“Hello MessageBox”)。 如果有的话,我加载了库以便 private void LoadPlugins(string path) { string[] files = Directory.GetFiles(path, "*.dll"); foreach (string fname in files) { A

请告诉我如何从dll调用方法,您甚至可以使用常用的原语MessageBox.Show(“Hello MessageBox”)。 如果有的话,我加载了库以便

    private void LoadPlugins(string path)
    {
        string[] files = Directory.GetFiles(path, "*.dll");

        foreach (string fname in files)
        {
            Assembly asm = Assembly.LoadFrom(fname);

            foreach (Type t in asm.GetExportedTypes())
            {
                if (typeof(PluginIface.IPlugin).IsAssignableFrom(t))
                {
                    PluginIface.IPlugin pi = (PluginIface.IPlugin)asm.CreateInstance(t.FullName);

                    pi.shwdialog(); // Here the window should appear)
                }
            }
        }
    }
该文件包含该接口

namespace PluginIface
{
    public interface IPlugin
    {
        void shwdialog();
    }
}
文件插件

namespace plg
{
    public class Plugin1 : IPlugin
    {
        public Plugin1()
        {
            System.Console.WriteLine("Plugin1 start");
        }

        public void shwdialog()
        {
            MessageBox.Show("Hello MessageBox");
        }
    }
}

您可以使用.dll添加引用。如果您计划使用插件体系结构,或者只是想知道如何从dll调用方法。没有帮助:(。错误出现在“foreach(在asm.GetExportedTypes()中键入t)”中,并添加了:异常类型“System.TypeLoadException”出现在mscorlib.dll中,但未在用户代码中处理其他信息:从程序集“loaddll,Version=1.0.0,Culture=neutral,PublicKeyToken=null”中没有以“loaddll.Plugin1”样式实现方法“shwdialog”。谢谢您的帮助,仅适用于Console.WriteLine(“Plugin1 start”);如果我添加MessageBox.Show(“Hello MessageBox”);它不编译插件,当前上下文中不存在“Error CS0103 name”MessageBox。“如果DLL是托管程序集,只需在项目中引用它。如果它是非托管的,则usw
DLLImport