Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. cannot invoke method via reflection...?

cannot invoke method via reflection...?

Scheduled Pinned Locked Moved C#
csharphelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkersch
    wrote on last edited by
    #1

    Hi there, i have a trie to invoke a method via reflection from Events.dll (the method that tries to create an instant of the class containing the method also resides in Events.dll) invoking method is : private void execute(string actionname,string[] parameters){ Assembly a = Assembly.LoadFrom("Events.dll"); Type[] ts = a.GetTypes(); Type t=a.GetType("IOW.NET.Events.StartProgram"); MethodInfo runmethod = t.GetMethod("run"); object obj=Activator.CreateInstance(t); runmethod.Invoke(obj,parameters); } Class StartProgram looks like this: namespace IOW.NET.Events { public class StartProgram: Event { public StartProgram(){} public void run(object[] parameters) { //do something } } } My Programs interrupts working when i call runmethod.Invoke(obj,parameters); any ideas where the error is? i just can´t find it. Events.dll is in the right directory, otherwise it would give me an error when i load the assembly from file. i can also read all types from the Assembly-object without problems. thanks for any hints best regards jkersch

    N H 2 Replies Last reply
    0
    • J jkersch

      Hi there, i have a trie to invoke a method via reflection from Events.dll (the method that tries to create an instant of the class containing the method also resides in Events.dll) invoking method is : private void execute(string actionname,string[] parameters){ Assembly a = Assembly.LoadFrom("Events.dll"); Type[] ts = a.GetTypes(); Type t=a.GetType("IOW.NET.Events.StartProgram"); MethodInfo runmethod = t.GetMethod("run"); object obj=Activator.CreateInstance(t); runmethod.Invoke(obj,parameters); } Class StartProgram looks like this: namespace IOW.NET.Events { public class StartProgram: Event { public StartProgram(){} public void run(object[] parameters) { //do something } } } My Programs interrupts working when i call runmethod.Invoke(obj,parameters); any ideas where the error is? i just can´t find it. Events.dll is in the right directory, otherwise it would give me an error when i load the assembly from file. i can also read all types from the Assembly-object without problems. thanks for any hints best regards jkersch

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      jkersch wrote: any ideas where the error is? i just can´t find it. I would suggest you put a check for null values along the way. Also, in particular, there is a overloaded method for creating a MethodInfo object that allows you to specify the BindingFlags via a bit mask, that could be your problem. - Nick Parker
      My Blog | My Articles

      1 Reply Last reply
      0
      • J jkersch

        Hi there, i have a trie to invoke a method via reflection from Events.dll (the method that tries to create an instant of the class containing the method also resides in Events.dll) invoking method is : private void execute(string actionname,string[] parameters){ Assembly a = Assembly.LoadFrom("Events.dll"); Type[] ts = a.GetTypes(); Type t=a.GetType("IOW.NET.Events.StartProgram"); MethodInfo runmethod = t.GetMethod("run"); object obj=Activator.CreateInstance(t); runmethod.Invoke(obj,parameters); } Class StartProgram looks like this: namespace IOW.NET.Events { public class StartProgram: Event { public StartProgram(){} public void run(object[] parameters) { //do something } } } My Programs interrupts working when i call runmethod.Invoke(obj,parameters); any ideas where the error is? i just can´t find it. Events.dll is in the right directory, otherwise it would give me an error when i load the assembly from file. i can also read all types from the Assembly-object without problems. thanks for any hints best regards jkersch

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        What Nick said is the solution to the most likely problem. If run is not public, then you must use the overload with BindingFlags.NonPublic. If it's a static method, then you have to include BindingFlags.Static (perform a bitwise OR with flags). Using just GetMethod(string) uses BindingFlags.Public | BindingFlags.Instance. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

        J 1 Reply Last reply
        0
        • H Heath Stewart

          What Nick said is the solution to the most likely problem. If run is not public, then you must use the overload with BindingFlags.NonPublic. If it's a static method, then you have to include BindingFlags.Static (perform a bitwise OR with flags). Using just GetMethod(string) uses BindingFlags.Public | BindingFlags.Instance. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          J Offline
          J Offline
          jkersch
          wrote on last edited by
          #4

          i finally found the error. my (public) run-method wanted to have object[] Myparameters as argument. i wanted to invoke it with methodobject.invoke(classobject,object[] MyParameters). i wrapped the object[]-array my method uses in a second object[] array i.e. methodobject.invoke(classobject,new object[]{object[] MyParameters}); and it worked out of the box, thank´s a lot nevertheless, jkersch

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups