Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,
I append some dll files to my program, and set these names as item to menustrip. I want when click on these items run dll automaticly. so, these dll files implement an interface dll!
C#
private void Form1_Load(object sender, EventArgs e) { foreach (var file in Directory.GetFiles(@"F:\tamrin\calcut\calcut\bin\Debug\plugin\", "*.dll")) { s = file.ToString(); var asm = Assembly.LoadFrom(file.ToString());
                foreach (var type in asm.GetTypes())
                {
                        AssemblyName asn = asm.GetName();
                        string[] b = asn.ToString().Split(',');
                        menu.Items.Add(b[0]);

// how to create instance from dll file and run its method?//
                    }
                }
            }
Posted

.NET framework (since 4) has a part that addresses extensiblity via interfaces - it called Managed Extensibility Framework[^]...
It enables you to define extension points (like menu) and load DLLs based on interfaces implemented by those DLLs...
I wrote an article about it that show the ides via a extensible web application: Extensible Web Application[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-May-15 11:54am    
MEF is one of the approaches, my 5, but I think that in many cases, the plug-in architecture developed from scratch will give much simpler and very reliable solution which is easy to use. I explained it in Solution 3, please see.
—SA
Try
C#
Type t = asn.GetType(file.ToString() + "ClassName");  //Classname is class to be instantiated
var methodInfo = t.GetMethod("MyMethod", new Type[] { typeof(byte[]), typeof(int) });//myMethod is method to be instantiated
var o = Activator.CreateInstance(t);
var result = methodInfo.Invoke(o, params);//invoke the method

Once the assembly is loaded, load the class and then invoke the method.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900