0

This is my class in a C# DLL

namespace MyNS
{
    public class MyClass
    {
        public static int Execute(string logKey, 
                                  string key, 
                                  XmlNode xmlPars)
        {
            return 0
        }
    }
}

I load DLL at run time and load Class Type in to _type variable. But when I call the function Execute from a Windows service like this:

counter = (int)_type.InvokeMember("Execute", 
                                  BindingFlags.Public |
                                  BindingFlags.InvokeMethod | 
                                  BindingFlags.Static, 
                                  null, 
                                  null, 
                                  new object[] { 
                                                  logKey, 
                                                  Key, 
                                                  _xmlParams 
                                                });

I get "Exception has been thrown by the target of an invocation."
What did I do wrong here?

Adinia
  • 3,722
  • 5
  • 40
  • 58
QuangND
  • 97
  • 1
  • 12
  • At what line the exception is thrown ? I tried a similar example it worked. Can you post how you load the assembly and create type? – Novice Dec 19 '12 at 07:48

1 Answers1

1

How you are loading the the DLL, can you please share the code to load the dlls dynamically.?

Here's the sample code.

Assembly assembly = Assembly.LoadFrom("ABC.dll");
object o = Activator.CreateInstance(assembly.GetType("ClassName"));
/// then invoke the method
Yahia
  • 69,653
  • 9
  • 115
  • 144
sohail.hussain.dyn
  • 1,411
  • 1
  • 16
  • 26
  • Thanks for your reply.
    I load DLL in the same way of yours. Every thing OK but I found that if DLL (my Plugin) reside in different directory of the serivice, and the DLL has references to others DLL. I have to copy other DLLs in to the directory of Windows service instead of Plugin directory.
    The problem is solved now.
    – QuangND Dec 19 '12 at 08:20