Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there.
how can i make a program that execute codes that typed by user?
for example in my form there is a textbox and when user type a c# code in it and press execute button, my program execute textbox's text as c# code.

thank you all dear friends.
Posted
Comments
[no name] 27-Jul-12 17:02pm    
Bad idea but look at the CodeDom namespace.
Abolfazl Beigi 27-Jul-12 17:03pm    
i just wana run my code from a textbox.
if you know that give me an example please.

You can use Roslyn. A Codeproject article covering using Roslyn for execution is coverer in Using Roslyn ScriptEngine for a ValueConverter to process user input[^]
 
Share this answer
 
Comments
Abolfazl Beigi 27-Jul-12 20:41pm    
Thank you my friend.
When someone tells you the name of something, it means you can type it in to google and find help. If you know what it's called and you can't google it, then you're not really learning how to be a programmer.

Here[^], I did it for you.
 
Share this answer
 
Comments
Abolfazl Beigi 27-Jul-12 17:15pm    
you are right dude. i just had a mistake to think about it.
thank you.
This is not so easy as Cristian told you. It will work once and more, but the problem is when you need to repeat the compilation of the code typed by the user multiple times if you load the freshly built assembly in your host application process again and again, as it is usually required. It will create a memory leak, which could be big. Here is why: there is a way to load yet another assembly, but there is no a way to unload it. This is designed so because unloading is unsafe: what is some code will call a method which does not exist anymore? Except one case: you can load it in a separate application domain and unload the whole domain.

So, you would need to create a new application domain each time the user wants to execute a new edited code, build and load assembly, call some method and then unload the application domain. But then you will need to work through the application domain boundary, as domain are isolated as well as separate processes. Such separation means using IPC, which is a hassle. Nevertheless, this is quite possible to implement.

Please see my past answers to related questions:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^];

As related to DOM and code parsing:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^];

This is about the simple part of it, when you don't need to reload assemblies:
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^].

—SA
 
Share this answer
 
Comments
Abolfazl Beigi 28-Jul-12 18:41pm    
Thank you for your complete answer and useful links.
Sergey Alexandrovich Kryukov 28-Jul-12 23:15pm    
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900