Click here to Skip to main content
15,887,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dll file containing the method, Project file that calls the method in dll, exception and my findings are below, I do not understand what I'm missing. Please help!

dll file:
C#
public class Class2
    {
        public string TestMethod1()
        {
            return "hello world!";
        }

        public void TestMethod2(int test)
        {
        }
    }

Project where Method in dll is called:
C#
try
            {
                Assembly assembly = Assembly.LoadFrom(@"D:\TestDll.dll");                
                foreach (Type ti in assembly.GetTypes().Where(x => x.IsClass))
                {
                    if (ti.Name == "Class2")
                    {
                        ti.GetMethod("TestMethod2");
                        MethodInfo[] method = ti.GetMethods();
                        Type t = assembly.GetType("TestDll.Class2");
                        BindingFlags flags = BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance;
                        object res = t.InvokeMember("TestMethod2", BindingFlags.InvokeMethod, null, t, new object[0]);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }

Exception:
MissingMethodException Occurred<br />
Method 'TestDll.Class2.TestMethod2' not found.


My Findings from MethodInfo:
method[1] = {Void TestMethod2(Int32)};<br />
method[1].Name = TestMethod2


If I change the last parameter i.e. input parameters following error occurs
Exception:
TargetException Occurred<br />
Object does not match target type.
Posted
Updated 20-Nov-17 2:37am

Try like this..


C#
if (ti.Name == "Class2")
           {
               MethodInfo TestMethod1 = ti.GetMethod("TestMethod1");
               MethodInfo TestMethod2 = ti.GetMethod("TestMethod2");
               var instance = Activator.CreateInstance(ti);
               string value = TestMethod1.Invoke(instance, null).ToString();
               TestMethod2.Invoke(instance, new object[] { 1 });

           }
 
Share this answer
 
Comments
Prabhu S M 10-Jan-14 6:45am    
Thanks Karthik! It's working, any idea how to invoke a method from an interface dll? I believe instance cannot be created for interface.
Karthik_Mahalingam 10-Jan-14 6:52am    
welcome solomon :)
yes you can
check this link
Prabhu S M 10-Jan-14 7:10am    
I don't understand it a bit Karthik :'(
can you explain using the code that I had given above.
Suppose Class2 class is implementing an Interface which has 2 methods and Class2 contains definition for both the methods declared in Interface i.e. TestMethod1 & TestMethod2.
How would I invoke the interface dll now?
Karthik_Mahalingam 10-Jan-14 7:17am    
that is of no use, any how you should need the instance of the class implementing the interface...
so the interface will not comes to picture.
got it ??
Prabhu S M 13-Jan-14 2:29am    
yes.. thanks Karthik!
Simple: your methods are not static, they are instance methods: you need to create an instance of the object and pass that through, rather than trying to pass the type of the class, and you need to pass the argument through:
C#
Assembly assembly = Assembly.LoadFrom(@"D:\TestDll.dll");
foreach (Type ti in assembly.GetTypes().Where(x => x.IsClass))
    {
    if (ti.Name == "Class2")
        {
        Type t = assembly.GetType("TestDll.Class2");
        object obj = t.InvokeMember(null,
                                    BindingFlags.DeclaredOnly |
                                       BindingFlags.Public |
                                       BindingFlags.NonPublic |
                                       BindingFlags.Instance |
                                       BindingFlags.CreateInstance,
                                    null,
                                    null,
                                    null);
        object res = t.InvokeMember("TestMethod2", BindingFlags.InvokeMethod, null, obj, new object[] { (object)13 });
        }
    }
 
Share this answer
 

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