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. Converting a variable to a method so it can be run (somewhat like Eval could do).

Converting a variable to a method so it can be run (somewhat like Eval could do).

Scheduled Pinned Locked Moved C#
tutorialcsharpjavascripthelpquestion
8 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.
  • A Offline
    A Offline
    Azad Giordano Ratzki
    wrote on last edited by
    #1

    Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(

    L D A 3 Replies Last reply
    0
    • A Azad Giordano Ratzki

      Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      Read about Reflection in .NET. http://msdn2.microsoft.com/en-us/library/f7ykdhsy(VS.80).aspx[^] In this page, you will see a paragrah beginning with Use MethodInfo to discover information .... This paragraph is your friend.

      ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

      1 Reply Last reply
      0
      • A Azad Giordano Ratzki

        Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(

        D Offline
        D Offline
        Dario Solera
        wrote on last edited by
        #3

        Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.

        If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

        A L 2 Replies Last reply
        0
        • A Azad Giordano Ratzki

          Can someone help me!? :) I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...! public static void RunAMethod(string methodname) { methodname(); //method to run } ///---- so I could do this from another method RunAMethod("runwhatevermethodIwant"); ///----- Is this possible??? :sigh: :(

          A Offline
          A Offline
          Azad Giordano Ratzki
          wrote on last edited by
          #4

          Thank you very much for both of your replies, I had a hunch that Reflection might be something I needed to look into, and I'll definitely give it a shot. :-)

          1 Reply Last reply
          0
          • D Dario Solera

            Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.

            If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

            A Offline
            A Offline
            Azad Giordano Ratzki
            wrote on last edited by
            #5

            Delegates seem very interesting to me also...although I am having a little difficulty visualizing their usage properly from the MSDN article, is there anyway you could write out a little snippet showing how I could use delegates to call a method whose name is based on a string I enter? :) Thanks!

            1 Reply Last reply
            0
            • D Dario Solera

              Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.

              If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #6

              Can you call a delegate with a method name defined in a string?!? Because this seems to be what he wants.

              ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

              D 1 Reply Last reply
              0
              • L Le centriste

                Can you call a delegate with a method name defined in a string?!? Because this seems to be what he wants.

                ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

                D Offline
                D Offline
                Dario Solera
                wrote on last edited by
                #7

                No. Not directly at least. The point is that invoking methods at runtime by their name is not very clean, in my opinion. Since I guess the methods he wants to invoke are parameterless and with no return value, using delegates would be very easy and would improve the robustness of the code.

                If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

                L 1 Reply Last reply
                0
                • D Dario Solera

                  No. Not directly at least. The point is that invoking methods at runtime by their name is not very clean, in my opinion. Since I guess the methods he wants to invoke are parameterless and with no return value, using delegates would be very easy and would improve the robustness of the code.

                  If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker My Blog - My Photos - ScrewTurn Wiki

                  L Offline
                  L Offline
                  Le centriste
                  wrote on last edited by
                  #8

                  I agree with you in the way that he should consider using an interface instead instead of calling a method by a name specified in a string.

                  ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

                  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