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. using delegate

using delegate

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

    I have a sample class as shown below
    public class MyAirport
    {
    delegate void ProcessAirport(string s);
    ProcessAirport pa;
    public MyAirport(string sAirportName)
    {
    pa = new ProcessAirport(ProcessMumbai);
    }
    private void ProcessMumbai(string s)
    {
    MessageBox.Show(s);
    }
    public void ProcessAirport()
    {
    pa("Hello Mumbai");
    }
    }

    in the class above i have used created a delegate instance with statement
    pa = new ProcessAirport(ProcessMumbai);
    where in i am passing 'ProcessMumbai' directly as a function pointer to ProcessAirport delegate.

    now my question is : can i pass function pointer referance by string like,

    pa = new ProcessAirport("ProcessMumbai");

    if so, please guide.

    thanks, vaibhav

    S 1 Reply Last reply
    0
    • D d_vaibhav

      I have a sample class as shown below
      public class MyAirport
      {
      delegate void ProcessAirport(string s);
      ProcessAirport pa;
      public MyAirport(string sAirportName)
      {
      pa = new ProcessAirport(ProcessMumbai);
      }
      private void ProcessMumbai(string s)
      {
      MessageBox.Show(s);
      }
      public void ProcessAirport()
      {
      pa("Hello Mumbai");
      }
      }

      in the class above i have used created a delegate instance with statement
      pa = new ProcessAirport(ProcessMumbai);
      where in i am passing 'ProcessMumbai' directly as a function pointer to ProcessAirport delegate.

      now my question is : can i pass function pointer referance by string like,

      pa = new ProcessAirport("ProcessMumbai");

      if so, please guide.

      thanks, vaibhav

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      AFAIK, you can't do that directly with delegates. You need to use reflection for that. Something like

      public class MyAirport
      {
      delegate void ProcessAirportDelegate(MethodInfo methodInfo);
      ProcessAirportDelegate pa;
      public MyAirport(string airportName)
      {
      pa = new ProcessAirportDelegate(ProcessAirport);
      }

      private void ProcessAirport(MethodInfo methodInfo)
      {
      methodInfo.Invoke(this, new object[] {}); // Your parameters, if any.
      }

      public void CallProcessMethod(string methodName)
      {
      MethodInfo methodInfo = this.GetType().GetMethod(methodName);
      pa(methodInfo);
      }
      }

      I created a delegate because your code had one. You can instead call methodInfo.Invoke directly from CallProcessMethod. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      D 1 Reply Last reply
      0
      • S S Senthil Kumar

        AFAIK, you can't do that directly with delegates. You need to use reflection for that. Something like

        public class MyAirport
        {
        delegate void ProcessAirportDelegate(MethodInfo methodInfo);
        ProcessAirportDelegate pa;
        public MyAirport(string airportName)
        {
        pa = new ProcessAirportDelegate(ProcessAirport);
        }

        private void ProcessAirport(MethodInfo methodInfo)
        {
        methodInfo.Invoke(this, new object[] {}); // Your parameters, if any.
        }

        public void CallProcessMethod(string methodName)
        {
        MethodInfo methodInfo = this.GetType().GetMethod(methodName);
        pa(methodInfo);
        }
        }

        I created a delegate because your code had one. You can instead call methodInfo.Invoke directly from CallProcessMethod. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        D Offline
        D Offline
        d_vaibhav
        wrote on last edited by
        #3

        hi Senthil, Thanks for the help. Now i have got the perfect clue. now i am not using gelegates, i am using reflection instade. thanks, vaibhav.

        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