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. Calling a function by it's name which has been stored in a string

Calling a function by it's name which has been stored in a string

Scheduled Pinned Locked Moved C#
debuggingtutorialquestion
4 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.
  • G Offline
    G Offline
    Gareth_Hastings
    wrote on last edited by
    #1

    Hello, I've found in every function I am writing I have lots of debug code and was just wondering if I can somehow shorten it. For example: void testFunction() { try { .... Do something that might cause an exception here } catch(mySpecialException e) { myLogClass.log(e.message); myLogClass.dump; //dumps systeminfo to somewhere } } What I would like is to have some kind of wrapper which I could pass the function name (either by name or as a pointer(delegate?)) to it which would then execute and return. Example: void wrapper(functionToRun()) { try { functionToRun(); } catch(...) { myLogClass.log(message); } } void test() { Console.WriteLine("Hello this is a test!"); } void hello() { Console.WriteLine("Hello"); } static void Main(string[] args) { wrapper(test()); wrapper(hello()); } Any ideas? Thanks

    S 1 Reply Last reply
    0
    • G Gareth_Hastings

      Hello, I've found in every function I am writing I have lots of debug code and was just wondering if I can somehow shorten it. For example: void testFunction() { try { .... Do something that might cause an exception here } catch(mySpecialException e) { myLogClass.log(e.message); myLogClass.dump; //dumps systeminfo to somewhere } } What I would like is to have some kind of wrapper which I could pass the function name (either by name or as a pointer(delegate?)) to it which would then execute and return. Example: void wrapper(functionToRun()) { try { functionToRun(); } catch(...) { myLogClass.log(message); } } void test() { Console.WriteLine("Hello this is a test!"); } void hello() { Console.WriteLine("Hello"); } static void Main(string[] args) { wrapper(test()); wrapper(hello()); } Any ideas? Thanks

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

      If all your functions have the same signature, then it is possible. You can do something like

      public class Foo
      {
      delegate void WrapperDelegate();

      void Hello() { Console.WriteLine("Hello"); }
      void Test() { Console.WriteLine("Test"); }
      
      public void SomeFunc()
      {
         executeFunction(new WrapperDelegate(Hello));
         executeFunction(new WrapperDelegate(Test));
      }
      
      private void executeFunction(WrapperDelegate d)
      {
          try
          {
             d();
          }
          catch(Exception e)
          {
             // blah blah
          }
      }
      

      }

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      G 1 Reply Last reply
      0
      • S S Senthil Kumar

        If all your functions have the same signature, then it is possible. You can do something like

        public class Foo
        {
        delegate void WrapperDelegate();

        void Hello() { Console.WriteLine("Hello"); }
        void Test() { Console.WriteLine("Test"); }
        
        public void SomeFunc()
        {
           executeFunction(new WrapperDelegate(Hello));
           executeFunction(new WrapperDelegate(Test));
        }
        
        private void executeFunction(WrapperDelegate d)
        {
            try
            {
               d();
            }
            catch(Exception e)
            {
               // blah blah
            }
        }
        

        }

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        G Offline
        G Offline
        Gareth_Hastings
        wrote on last edited by
        #3

        Thanks for the suggestion but unfortunately hardly any of my functions have the same signature!

        S 1 Reply Last reply
        0
        • G Gareth_Hastings

          Thanks for the suggestion but unfortunately hardly any of my functions have the same signature!

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

          The solution will work if there only a few signatures. You just need to create one WrapperDelegate for each type and add one executeFunction for each such WrapperDelegate. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          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