Share via


How can I convert from string to code in C#

Question

Thursday, March 21, 2013 8:18 PM

Hi all

if i just have this code

string myString="messeagebox.show("hello");"

how can I execute these code using the string in easy and simple way

like this

exexute(myString);

All replies (3)

Thursday, March 21, 2013 8:32 PM âś…Answered

What it seems you are trying to do is create a delegate, where your Execute method accepts a pointer to another method and executes it.

You can't do that with a string. I suggest you look into Delegates and Predicates

Why do we need C# delegates

Delegate Tutorial

Understanding Predicate Delegates in C#

Another alternative to implementing what you seem to desire is called the Command Pattern. The Command Pattern follows something similar to the following structure:

static void Main()
    {
      // Create receiver, command, and invoker
      Receiver receiver = new Receiver();
      ICommand command = new ConcreteCommand(receiver);
      Invoker invoker = new Invoker();
 
      // execute the command
      invoker.Execute(command);
 
      // Wait for user
      Console.ReadKey();
    }

The Receiver knows how to perform the operations associated with carrying out a request.

The Invoker asks the command to carry out a request.

The ICommand declares an interface for executing an operation

The ConcreteCommand defines a binding between a Receiver object and an action and implements Execute by invoking the corresponding operation on Receiver.  

In this structure, the Command is not executed via delegation, but via a concrete implementation of the ICommand interface  In this case, ICommand would likely have a single method requirement of "Execute()" and through a concept known as inversion of control, your Execute(command) method would be responsible for calling command.Execute() that it knows exists on the command because it implements the ICommand interface)

If you wish to take a Command Pattern approach, I suggest you read up on application design patterns.

Both a delegate structure and a command pattern implementation are "easy" and "simple" ways to bind the execution of a method to some action/event/trigger and delay the execution of the method until the required action/event/trigger has occurred.


Thursday, March 21, 2013 9:05 PM

Hi,

Alan said it right. Delegates are easiest way to accomplish that. Here is the sample code to do that

        public delegate void ShowMeOnMessageBoxDelegate(string msg);
        public event ShowMeOnMessageBoxDelegate DisplayEventHandler;

        public Form1()
        {
            InitializeComponent();
            this.DisplayEventHandler += MyMessageBoxHandler;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DisplayEventHandler("Wow Button 1 clicked");
        }

         private void button2_Click(object sender, EventArgs e)
         {
             DisplayEventHandler("Wow Button 2 clicked");
         }

         private void MyMessageBoxHandler(string msg)
         {
             MessageBox.Show(msg);
         }

Friday, March 22, 2013 3:42 AM

There is no easy way.  Firstly, you would need to spell 'message' correctly.  Next you would need to get the capitalization of MessageBox correct.  Then you would need to escape the double quotes around the parameter.

After that you need to investigate something like Roslyn to compile your fragment and then run it.

I'm guessing that this is not what you really want to do because it would seem like your intention with this example is to show the word 'hello' and force the user to click an OK button.  There are easier ways to do that.

So, what is the business requirement that you are trying to solve?  I am pretty sure there will be an easy way to solve your actual problem.  Tell us what you are really trying to achieve.

Paul Linton