Adam K Dean
Adam K Dean

Reputation: 7475

Is there a way I can parse/run C# within an application, almost like a script?

I am currently writing some code and I am wondering if it would be possible to execute some C# code from my application. I'll give you an idea, lets say I want to give the user a textbox and have them type some code and hit go, I want them to ask for a list of fruit and then go through each fruit and output it.. an example:

var fruitList = getFruit();
foreach(var fruit in fruitList)
{
   print(fruit.Name);
}

I would like to be able to go through this and assign a list of Fruit objects to fruitList, the parser should be able to tie up getFruit() to a method I've written in the c# code. The same goes for print, it should equate this to a print function I've written that outputs it to a textbox.

Now I know that C# isn't a script, it is compiled, and I've done a lot of Googling but can't really find anything. My only option to me appears to be to write a little language parser myself - which sounds fun - but I've done this before and I know it's hard work. So this is just a preliminary check to see if some solution does exist before I commit to the long haul.

So, my fellow programmers, do you know of anything that may be able to assist me?

If not, no problem, I appreciate all feedback whether it's tips, advice, links to articles such as this http://blogs.msdn.com/b/ericwhite/archive/2010/06/29/writing-a-recursive-descent-parser-using-c-and-linq.aspx or a solution.

Regards,
Adam

EDIT: I have managed to get a working example. Note this code is a bit messy as I've pasted some other code in to the test app, but it works. Basically, I compile the code into a DLL, then I load the DLL, find the type, find the method, and invoke it. It's pretty damn quick too! I don't want to spam you so the full code is below:

http://imdsm.blogspot.com/2012/01/compile-c-into-assembly-then-load-it.html

Thank you to everyone who posted here. You just saved me days of confusion!

Upvotes: 3

Views: 441

Answers (4)

Andreas
Andreas

Reputation: 6465

You mean like in this screenshot here?

We use this in our software, HeuristicLab, you can add a ProgrammableOperator into an operator graph which will execute the code that you typed in at the place that you typed it in a custom-built algorithm.

In the System.CodeDom namespace you've got all you need to dynamically compile code. You can create an assembly from the compilation, get the assembly's types and execute their code.

Upvotes: 3

Carl Bergquist
Carl Bergquist

Reputation: 3942

You can use the CSharpCodeProvider

Or you could use a scripting language and run the interpreter in your application Ex IronRuby

Upvotes: 1

munissor
munissor

Reputation: 3785

Have a look if Roslyn can help , it's still in CTP status.

Upvotes: 1

SynerCoder
SynerCoder

Reputation: 12766

I think the new compiler project of microsoft is what you are looking for. With it you can run C# as were a script indeed.

Project "Roslyn"

Upvotes: 1

Related Questions