Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way I can parse/run C# within an application, almost like a script?
    primarykey
    data
    text
    <p>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:</p> <pre><code>var fruitList = getFruit(); foreach(var fruit in fruitList) { print(fruit.Name); } </code></pre> <p>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.</p> <p>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.</p> <p>So, my fellow programmers, do you know of anything that may be able to assist me?</p> <p>If not, no problem, I appreciate all feedback whether it's tips, advice, links to articles such as this <a href="http://blogs.msdn.com/b/ericwhite/archive/2010/06/29/writing-a-recursive-descent-parser-using-c-and-linq.aspx" rel="nofollow">http://blogs.msdn.com/b/ericwhite/archive/2010/06/29/writing-a-recursive-descent-parser-using-c-and-linq.aspx</a> or a solution.</p> <p>Regards,<br> Adam</p> <p>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:</p> <p><a href="http://imdsm.blogspot.com/2012/01/compile-c-into-assembly-then-load-it.html" rel="nofollow">http://imdsm.blogspot.com/2012/01/compile-c-into-assembly-then-load-it.html</a></p> <p>Thank you to everyone who posted here. You just saved me days of confusion!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload