Compiling code to dynamic method

A

Andrus

How to compile source code into dynamic method ?

System.Reflection.Emit.DynamicMethod requires IL code for creation.
How to use C# source code to create method instead of manually creating IL
code ?

Assembly compilation requires passing parameters to access calling assembly
members. Assemblies cannot unloaded.
Dynamic method has automatic acces to assembly members. Memory allocated by
dynamic code can be re-used if code is released.

Andrus.
 
P

Pavel Minaev

How to compile source code into dynamic method ?

System.Reflection.Emit.DynamicMethod  requires IL code for creation.
How to use C# source code to create method instead of manually creating IL
code ?

There's no such thing, if only because C# language does not allow for
free-standing methods.

If you can share more details on why you think you need this
particular feature, then some alternative approaches might be
proposed. It might be that what you want to do is easy if you use
expression trees and Expression.Compile() from C# 3.0, for example.
 
A

Andrus

There's no such thing, if only because C# language does not allow for
free-standing methods.
If you can share more details on why you think you need this
particular feature, then some alternative approaches might be
proposed. It might be that what you want to do is easy if you use
expression trees and Expression.Compile() from C# 3.0, for example.

I need to add lightweight scripting capability to my application.

Creating assemblies requires to design interfaces passed to scripts.
Assemblies cannot be unloaded in same appdomain, parameters cannot easily
passed
to other appdomain.

So I want to create scripts as current assembly methods.

Andrus.
 
P

Pavel Minaev

I need to add lightweight scripting capability to my application.

Creating assemblies requires to design interfaces passed to scripts.
Assemblies cannot be unloaded in same appdomain, parameters cannot easily
passed
to other appdomain.

So I want to create scripts as current assembly methods.

How about using JScript.NET and its eval() function? Admittingly, I do
not know how exactly it is implemented, but given its nature and
frequent use in JavaScript it can hardly be done by generating a new
assembly every time. Now, eval() is a built-in function, and you
cannot call it from C# directly, but you can write an assembly in
JScript that wraps a call to eval() into a plain .NET class. Something
like this:

// Evaluator.js
import System;

public class Evaluator
{
function Eval(script : String) : Object
{
return eval(script);
}
}

Compile it with "jsc /t:library", and then add a reference to the
resulting assembly to your application. The only caveat is that you'll
also need to reference Microsoft.JScript assembly. After that, the
usage is straightforward. Here's a simple JScript REPL in C#:

// Program.cs
using System;
using Microsoft.JScript;

class Program
{
static void Main()
{
var evaluator = new Evaluator();
while (true)
{
Console.Write("> ");

var script = Console.ReadLine();
if (script == string.Empty)
{
break;
}

try
{
var result = evaluator.Eval(script);
Console.WriteLine(result);
}
catch (JScriptException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}

Try entering this at the prompt to test it:

for (var i = 0; i < 10; ++i) System.Console.WriteLine(i)

Another problem with that approach is that in JScript, you need to
import a namespace (using "import" statement) before you can use any
types from it. Worse, code inside eval() cannot import anything - it
can only use namespaces that were imported at the point where eval()
itself was called - in this case, in Evaluator.js. So, you'll need to
import all namespaces you want to access in your script from there.
 
A

Andrus

How about using JScript.NET and its eval() function? Admittingly, I do
not know how exactly it is implemented, but given its nature and
frequent use in JavaScript it can hardly be done by generating a new
assembly every time. Now, eval() is a built-in function, and you
cannot call it from C# directly, but you can write an assembly in
JScript that wraps a call to eval() into a plain .NET class. Something
like this:

This requires writing expressions in javascript insed of C# ?
So my users should lean new language only for scripting ?
How to execute script with eval ?
As I know it takes only expression, not a script ?
This seems too hard.
I'm looking for C# scripting.

Andrus.
 
P

Pavel Minaev

This requires writing expressions in javascript insed of C# ?

Yes, but they are close enough. Any logical and arithmetic expression
will look the same. if/while/for/switch are the same.
So my users should lean new language only for scripting ?

It makes sense to use a scripting language for scripting. C# is not a
scripting language. ECMAScript is designed as a scripting language.
How to execute script with eval ?

I gave an example in my earlier post.
As I know it takes only expression, not a script ?

No, it takes an almost arbitrary script. You can declare variables and
functions in it, use arbitrary statements etc.
This seems too hard.
I'm looking for C# scripting.

C# is not a scripting language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top