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. Passing in Function name to be called

Passing in Function name to be called

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 4 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.
  • O Offline
    O Offline
    obelisk29
    wrote on last edited by
    #1

    Is there any way to have a function that accepts a function name as a parameter and is then able to call it? EXAMPLE: public void MyFunc(Function func) { func(); } Help is greatly appreciated.

    A N C 3 Replies Last reply
    0
    • O obelisk29

      Is there any way to have a function that accepts a function name as a parameter and is then able to call it? EXAMPLE: public void MyFunc(Function func) { func(); } Help is greatly appreciated.

      A Offline
      A Offline
      azusakt
      wrote on last edited by
      #2

      May be you can use Delegate and pass out delegate function.

      1 Reply Last reply
      0
      • O obelisk29

        Is there any way to have a function that accepts a function name as a parameter and is then able to call it? EXAMPLE: public void MyFunc(Function func) { func(); } Help is greatly appreciated.

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

        The following is a quick example of a delegate in C#.

        using System;
        
        namespace Test
        {
        	class Class1
        	{
        		delegate void Func(int i);
        
        		static void Main(string[] args)
        		{
        			Func f = new Func(SomeMethod);
        			f(4);
        		}
        
        		public static void SomeMethod(int i)
        		{
        			Console.WriteLine("Hello from SomeMethod. " +
        					  i + " was passed in.");
        			Console.Read();
        		}
        	}
        }
        

        -Nick Parker DeveloperNotes.com

        1 Reply Last reply
        0
        • O obelisk29

          Is there any way to have a function that accepts a function name as a parameter and is then able to call it? EXAMPLE: public void MyFunc(Function func) { func(); } Help is greatly appreciated.

          C Offline
          C Offline
          Corinna John
          wrote on last edited by
          #4

          Call the funcitzon "by name": public void MyFunc(Function func) { //get the type of the object containing the method Type type = this.GetType(); //get the method System.Reflection.MethodInfo methodInfo = type.GetMethod(func, BindingFlags.Default); String result; if(methodInfo == null){ result = TextConstants.MSG_MethodNotFound.Replace("Method not found"); }else{ //call the method result = methodInfo.Invoke(this, null).ToString(); } }

          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