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
  1. Home
  2. General Programming
  3. C#
  4. Reflection and Delegates

Reflection and Delegates

Scheduled Pinned Locked Moved C#
tutorialquestion
2 Posts 1 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.
  • W Offline
    W Offline
    wayward
    wrote on last edited by
    #1

    Is it possible given two strings (class name and method name) to dynamically create an instance of the class and get a delegate pointing to the method? For example (almostcode): public delegate void ActionHandler(Message m); string className = "Exony.Runtime.StateServices.Test.States.Stopped"; string actionName = "OnStart"; Type t = Type.GetType(className); object instance = t.Assembly.CreateInstance(t.FullName); ActionHandler handler = ?CREATE_DELEGATE?(instance, actionName); // Add the handler to an event map. fsm.Handlers += handler; Thanks in advance, James.

    W 1 Reply Last reply
    0
    • W wayward

      Is it possible given two strings (class name and method name) to dynamically create an instance of the class and get a delegate pointing to the method? For example (almostcode): public delegate void ActionHandler(Message m); string className = "Exony.Runtime.StateServices.Test.States.Stopped"; string actionName = "OnStart"; Type t = Type.GetType(className); object instance = t.Assembly.CreateInstance(t.FullName); ActionHandler handler = ?CREATE_DELEGATE?(instance, actionName); // Add the handler to an event map. fsm.Handlers += handler; Thanks in advance, James.

      W Offline
      W Offline
      wayward
      wrote on last edited by
      #2

      Doh.... If you look hard enough there is nearly always something in the FCL to do the job... :-O ActionHandler handler = Delegate.CreateDelegate(typeof(ActionHandler), instance, actionName) Thanks anyway... James.

      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