Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to execute a code which is stored a s a string in c#?

example:
string str_test="MessageBox.Show("H World")";


i want to run the code which is stored in str_test??


thanks in advance guys..
Posted
Comments
manognya kota 6-Feb-12 6:26am    
What is meant by run the code? do you want to execute any query? or You want to check what is the value stored in that variable?
BobJanova 6-Feb-12 7:26am    
Christian's answer is technically correct. However, I must question why you want to do this in C#. It's usually a sign of bad design.

Warning!

Unfortunately, the article Cristian referenced is all wrong. It has a fundamental flaw — a memory leak due to the fact that it is impossible to unload a loaded assembly. I added my comment to the article warning everyone against using it.

Also, one of the members insists that this article plagiarizes his work. Please look at the comment to the article: one of the commenter offered much better article: http://www.west-wind.com//presentations/dynamicCode/DynamicCode.htm[^].

Also, I explain the problem of unloading and describe the solution using System.AppDomain which is probably the only correct approach. Please see:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

—SA
 
Share this answer
 
Here[^] is a Code Project article on this.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 1:20am    
Cristian, perhaps you did not pay attention for the problems of this article. I checked it up and found totally unacceptable. (I did not vote, because this is not very much of your fault -- you just trusted the author, but you should not, after my warning.)

Please see my answer where I explain it and advise how to do it right.

Thank you very much.
--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