views:

126

answers:

6

Hello everyone! I have seen online how C# allows you to compile code on the fly and I think that is a really awesome thing. I need to use this functionality for my project. But here is the problem. I'm not trying to compile a whole function or new program dynamically. I just need to call a single statement to create or delete stuff on the fly.

Is there a way C# can compile and/or run a single code statement and allow that statement to act on variables and objects in the current program's scope?

Thanks

+2  A: 

You could compile C# using Microsoft.CSharp.CSharpCodeProvider, but that gets really complicated if you want to do it correctly, since you need to load your code in a separate App Domain to prevent memory leaks.

I'd suggest using IronPython or some other DLR language: http://www.codeplex.com/wikipage?ProjectName=IronPython

Some sample here, not sure how up-to-date it is but the idea's pretty much the same: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml

Rei Miyasaka
A: 

"I just need to call a single statement to create or delete stuff on the fly." Statements like these make me shudder down to my bones. What are you trying to accomplish here, really? You want to have your user write C# statements and have your program execute them within its AppDomain? Not only is this an immense security risk, it is also a terrible user experience.

Furthermore, C# is not a scripting language. If you try to shoehorn it into being one, You're Gonna Have A Bad Time (TM).

James Dunne
James: It depends on who the user is. VS has no problems running user-written C# code in a debug watch window or an immediate window. Presumably the OP is writing an app where the code gets input by the user, rather than downloaded off the Internet.
Gabe
I think even if that were the case there are still very legitimate uses for it, and C# with CAS is just as secure as, say, JavaScript running in a browser. Nonetheless, yeah, unless he *really* wants C#, I'd use a DLR language instead. http://en.wikipedia.org/wiki/Code_Access_Security
Rei Miyasaka
@Rei: good points. I agree, a DLR solution or some other language built for scriptability should be used instead.
James Dunne
A: 

You can, while using the debugger. At a breakpoint, just type some code into the Immediate Window in VS, and viola.

One of the best debugging features there are!

leppie
A: 

Scripting static languages comes trade-offs. There are scope and security concerns to consider, but they can be controlled.

If you're looking to execute static code (i.e. C#) from within a managed run time, I'd recommend starting with Mono. The Mono team has made strides creating a safe environment to compile JIT code into native code at run time.

Start with their official post on the subject.

Acoustic
A: 

It depends on what you are trying to do ... if you are looking to use it as an embedded scripting language within another application, then my answer doesn't apply, but if you just want to execute random C# statements (or programs if you like) and save them as scripts, LinqPad is awesome for that.

Richard Hein
A: 

To give you an alternative rather than using C# as a scripting language, have a look at Conscript [1].
Conscript is a compiled scripting language for .NET.

[1]: http://www.codeproject.com/KB/cs/Conscript.aspx

Dennis