コード例 #1
0
        //var files = Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath), "*.dll", SearchOption.AllDirectories);
        //List<myClass> commands = new List<myClass>();
        //foreach (string file in files)
        //{
        //    i++;
        //    Assembly commandAssembly = Assembly.Load(Path.GetFileNameWithoutExtension(file));
        //    if (commandAssembly != null)
        //    {
        //        foreach (Type type in commandAssembly.GetTypes())
        //        {
        //            if (type.BaseType != null && type.BaseType.FullName == typeof(myClass).FullName)
        //            {
        //                commands.Add((myClass)Activator.CreateInstance(type));
        //            }
        //        }
        //    }
        //}

        private void Form1_Load(object sender, System.EventArgs e)
        {
            var                  files      = Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath), "*.dll", SearchOption.AllDirectories);
            List <ImyClass>      commands   = new List <ImyClass>();
            List <IPlugin>       assemblies = new List <IPlugin>();
            List <ToolStripItem> mMenu      = new List <ToolStripItem>();

            foreach (string file in files)
            {
                IPlugin  plug            = null;
                Assembly commandAssembly = Assembly.Load(Path.GetFileNameWithoutExtension(file));
                if (commandAssembly != null)
                {
                    Type ObjType = null;
                    ObjType = commandAssembly.GetType(Path.GetFileNameWithoutExtension(file) + ".PlugIn");
                    if (ObjType != null)
                    {
                        plug      = (IPlugin)Activator.CreateInstance(ObjType);
                        plug.Host = this;
                        //plug.Menu = "I sent this from main";
                        Console.WriteLine("ipi.Add(1,2)=" + plug.Add(1, 2));
                        assemblies.Add(plug);
                        //For classes
                        List <ToolStripItem> sMenuList = new List <ToolStripItem>();

                        //get all clases implementing Itest
                        //var types = AppDomain.CurrentDomain.GetAssemblies()
                        //    .SelectMany(s => s.GetTypes())
                        //    .Where(p => typeof(ImyClass).IsAssignableFrom(p) && p.IsInterface == false);
                        var types = commandAssembly.GetTypes()
                                    .Where(p => typeof(ImyClass).IsAssignableFrom(p) && p.IsInterface == false);
                        foreach (Type type in types)
                        {
                            ImyClass myCls = (ImyClass)Activator.CreateInstance(type);
                            commands.Add(myCls);
                            sMenuList.Add(myCls.Menu);
                            //   plug.Menu.DropDownItems.Add(myCls.Menu);
                        }
                        // ToolStripMenuItem ms = new ToolStripMenuItem("ab", null, sMenu.ToArray());
                        plug.Menu.DropDownItems.AddRange(sMenuList.ToArray());

                        mMenu.Add(plug.Menu);
                    }
                }
            }
            this.menuStrip1.Items.AddRange(mMenu.ToArray());
        }
コード例 #2
0
        private void LoadPlugins()
        {
            var                  files    = Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath), "*.dll", SearchOption.AllDirectories).Where(f => f.Contains("PlugIn.dll") == false).ToArray();
            List <ImyClass>      commands = new List <ImyClass>();
            List <ToolStripItem> mMenu    = new List <ToolStripItem>();

            foreach (string file in files)
            {
                IPlugin  plug            = null;
                Assembly commandAssembly = Assembly.Load(Path.GetFileNameWithoutExtension(file));
                if (commandAssembly != null)
                {
                    Type ObjType = null;
                    try{
                        var typeo = commandAssembly.GetTypes()
                                    .Where(p => typeof(IPlugin).IsAssignableFrom(p) && p.IsInterface == false);
                        ObjType = typeo.ElementAt(0);
                    }catch {}
                    // ObjType = commandAssembly.GetType(Path.GetFileNameWithoutExtension(file) + ".PlugIn");
                    if (ObjType != null)
                    {
                        plug      = (IPlugin)Activator.CreateInstance(ObjType);
                        plug.Host = this;

                        assemblies.Add(plug);
                        //For classes
                        List <ToolStripItem> sMenuList = new List <ToolStripItem>();
                        //get all clases implementing Itest
                        //var types = AppDomain.CurrentDomain.GetAssemblies()
                        //    .SelectMany(s => s.GetTypes())
                        //    .Where(p => typeof(ImyClass).IsAssignableFrom(p) && p.IsInterface == false);
                        var types = commandAssembly.GetTypes()
                                    .Where(p => typeof(ImyClass).IsAssignableFrom(p) && p.IsInterface == false);
                        foreach (Type type in types)
                        {
                            ImyClass myCls = (ImyClass)Activator.CreateInstance(type);
                            commands.Add(myCls);
                            sMenuList.Add(myCls.Menu);
                        }
                        plug.Menu.DropDownItems.AddRange(sMenuList.ToArray());
                        mMenu.Add(plug.Menu);
                    }
                }
            }
            this.menuStrip1.Items.AddRange(mMenu.ToArray());
        }