Execute User written Code
-
I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve:
private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ }
I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.Ketty
-
I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve:
private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ }
I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.Ketty
-
You can't put code into an existing method. You have to compile the code into a new assembly. Take a look at the
Microsoft.CSharp.CSharpCodeProvider
class.--- single minded; short sighted; long gone;
Thanks, I did see an example of "Microsoft.CSharp.CSharpCodeProvider class" on CodeProject, if you or any body has used it. Is it performance promising?
Ketty
-
I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve:
private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ }
I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.Ketty
private void ExecuteUser()//Function in Utility { if(txtUserCode.Text != "while(true){}"){ //run! } }
Feel free to use this as a safeguard after you get the rest of it figured out :)
-
private void ExecuteUser()//Function in Utility { if(txtUserCode.Text != "while(true){}"){ //run! } }
Feel free to use this as a safeguard after you get the rest of it figured out :)
I am not quite sure if I understand the idea completely. Please advice.
Ketty
-
I am not quite sure if I understand the idea completely. Please advice.
Ketty
Just a joke about the dangers of letting users execute their own code -- the code I wrote "checks" for an infinite loop.
-
Just a joke about the dangers of letting users execute their own code -- the code I wrote "checks" for an infinite loop.
:-DJokes are fine but I am looking for serious comments on this issue
Ketty
-
I need to write a performance driven utility that allows user to write C# code in a text area. When they click on execute to see what is the final out, utility should complie and run the code written by user as if it is a function body. e.g. I am trying to achieve:
private void ExecuteUser()//Function in Utility { /* -------Body of this function will be filled by User Code from text area at click of button. */ }
I am not a C# coder for past few years:((, so any code snippet would be awesome. We are using .Net 2.0. VS 2005.Ketty
If you just want to compile code and execute it, you can always invoke the compiler with the correct command line options when the user would like to execute the code, and then start / wait on the process that you just compiled. That's probably the most general way to do things. I wouldn't advise it though, as I don't see any practical application. If, on the other hand, you want to do some kind of scripting, you could always encapsulate existing functions and dynamically call them by parsing a script, using a virtual address space within your actual program for the storage of stack variables and dynamically allocated memory.