locked
Using a string as code for Method RRS feed

  • Question

  •   private void button1_Click(object sender, EventArgs e)
    
      {
    
       string input = textBox1.Text;
    
       input(); //where input is the string 'run(hello, 5)'
    
      }
    
    
    
      public void run(string param, int x)
    
      {
    
       MessageBox.Show("Show " + param + " and " + x);
    
      }
    
    
    I would like to insert "Run("Hello",5)" into the textbox. Then when I click on the button it should run the run method. The question is can I somehow

     take the string and have it run as code in the application.
    Thursday, November 11, 2010 10:55 PM

Answers

  • Yes switch off of the string and execute what is needed by calling the target function.

    William Wegerson (www.OmegaCoder.Com)
    • Proposed as answer by Mike Dos Zhang Tuesday, November 16, 2010 4:46 AM
    • Marked as answer by Mike Dos Zhang Thursday, November 25, 2010 3:05 PM
    Thursday, November 11, 2010 11:03 PM
  • Hi vaadadmin2010,

    Welcome to MSDN Forums!

     

    You also can use invoke method to dynamic invoke a method by strings.

    Dynamically Invoke A Method, Given Strings with Method Name and Class Name

    public static string InvokeStringMethod2
     (string typeName, string methodName, string stringParam)
    {
     // Get the Type for the class
     Type calledType = Type.GetType(typeName);
    
     // Invoke the method itself. The string returned by the method winds up in s.
     // Note that stringParam is passed via the last parameter of InvokeMember,
     // as an array of Objects.
     String s = (String)calledType.InvokeMember(
         methodName,
         BindingFlags.InvokeMethod | BindingFlags.Public | 
          BindingFlags.Static,
         null,
         null,
         new Object[] { stringParam });
    
     // Return the string that was returned by the called method.
     return s;
    }
    

    Type.InvokeMember Method

    You can read this document to know more about invoke in C#.

     

    If there’s anything unclear, please feel free to let me know.

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

     

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    =================================================

    Third party disclaimer

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

     

    • Marked as answer by Mike Dos Zhang Thursday, November 25, 2010 3:05 PM
    Tuesday, November 16, 2010 4:54 AM
  • Hi,

    You should use reflexion : see here

    Regards,

     

    • Marked as answer by Mike Dos Zhang Thursday, November 25, 2010 3:06 PM
    Tuesday, November 16, 2010 9:17 AM

All replies

  • Yes switch off of the string and execute what is needed by calling the target function.

    William Wegerson (www.OmegaCoder.Com)
    • Proposed as answer by Mike Dos Zhang Tuesday, November 16, 2010 4:46 AM
    • Marked as answer by Mike Dos Zhang Thursday, November 25, 2010 3:05 PM
    Thursday, November 11, 2010 11:03 PM
  • Hi vaadadmin2010,

    Welcome to MSDN Forums!

     

    You also can use invoke method to dynamic invoke a method by strings.

    Dynamically Invoke A Method, Given Strings with Method Name and Class Name

    public static string InvokeStringMethod2
     (string typeName, string methodName, string stringParam)
    {
     // Get the Type for the class
     Type calledType = Type.GetType(typeName);
    
     // Invoke the method itself. The string returned by the method winds up in s.
     // Note that stringParam is passed via the last parameter of InvokeMember,
     // as an array of Objects.
     String s = (String)calledType.InvokeMember(
         methodName,
         BindingFlags.InvokeMethod | BindingFlags.Public | 
          BindingFlags.Static,
         null,
         null,
         new Object[] { stringParam });
    
     // Return the string that was returned by the called method.
     return s;
    }
    

    Type.InvokeMember Method

    You can read this document to know more about invoke in C#.

     

    If there’s anything unclear, please feel free to let me know.

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

     

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    =================================================

    Third party disclaimer

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

     

    • Marked as answer by Mike Dos Zhang Thursday, November 25, 2010 3:05 PM
    Tuesday, November 16, 2010 4:54 AM
  • Hi,

    please using following code snippet,

     private void button1_Click(object sender, EventArgs e)

      {

          string param = "hello"; int x = 5;
          textBox1.Text = "run("+ param+","+ x.ToString()+")";

          run(param ,x ); //where input is the string 'run(hello, 5)'

      }

    Tuesday, November 16, 2010 8:30 AM
  • Hi,

    You should use reflexion : see here

    Regards,

     

    • Marked as answer by Mike Dos Zhang Thursday, November 25, 2010 3:06 PM
    Tuesday, November 16, 2010 9:17 AM