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!