Fredrick
Fredrick

Reputation: 513

String to method

Being able to create javascript code on the fly is pretty cool. That is by using

HtmlPage.Window.Eval("alert('Hello World')");

But is there a way to do the same thing, with a C# method? Lets say something like

void MethodEval("MessageBox.Show('Hello World')");

Is it even possible, without having to recompile your code?

Upvotes: 1

Views: 550

Answers (6)

mP.
mP.

Reputation: 18266

Why do you need this feature ? Shouldnt your code know its paths and include that logic ? If you cant forsee such use cases - then perhaps its not needed. The only real benefit is it opens an opportunity to be abused and attacked. It sounds like your in directly creating a potential for an exploit - its a shame because managed runtimes like Java/CLR dont allow code injection but you are bringing all that goodness back in...

Upvotes: 0

Fredrick
Fredrick

Reputation: 513

Actually I kept on getting an error "unexpected indent" so changing the code to

PythonEngine pythonEngine = new PythonEngine();
            string script  = @"import clr; clr.AddReference(""System.Windows"");"; 
                   script += @"import System.Windows as Wins;";
                   script += @"Wins.MessageBox.Show(""Hello World"");";
            pythonEngine.Execute(script);

Worked! interesting... Thanks

Upvotes: 0

sipsorcery
sipsorcery

Reputation: 30699

You can do it right now. The ag DLR (Silverlight Dynamic Languages Runtime) can host javascript.

While Javascript cannot be hosted with the DLR outside the browser Ruby and Python can. Here's an example of a C# snippet using the DLR and hosting a piece of Python of code to demonstrate.

using IronPython.Hosting;

PythonEngine pythonEngine = new PythonEngine();
string script = @"import clr 
 clr.AddReference(""System.Windows.Forms"") 
 import System.Windows.Forms as WinForms 
 WinForms.MessageBox.Show(""Hello"", ""Hello World"")";
pythonEngine.Execute(script);

Upvotes: 1

Matthew Olenik
Matthew Olenik

Reputation: 3577

If you don't mind Boo, you can use its interpreter.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415630

It's possible using tricks posted by others. However, it's usually a very bad idea.

.Net code typically runs in a more trusted context than a javascript browser sandbox, and has access to a much richer, and therefore potentially damaging, api.

Instead you use the System.Addin namespace to provide a very strict interface for extensions, plugins, and the like. If you're just trying to use a more "fluid" or functional programming environment you can use fun features like lamdba expressions and closures to pass functionality around internally.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564363

This is possible, but a little more tricky, using Microsoft's .NET framework.

The C# compiler is part of the base runtime, so you can compile an in-memory assembly, and execute code in there on the fly.

Here is a good MSDN blog post describing the basic process.

I have used this before to make a scripting engine for a C# project. With a little work wrapping this, you can make this quite easy to use. An open source project I've worked on has a project dedicated to this: Pluto.Scripting

We had examples and tests in that project which show dynamic compilation and execution of C#, VB.NET, and Boo.

Upvotes: 1

Related Questions