468,747 Members | 1,669 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,747 developers. It's quick & easy.

Compiling code to dynamic method

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.

Jun 27 '08 #1
5 2352
On Jun 22, 1:00*am, "Andrus" <kobrule...@hot.eewrote:
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.
Jun 27 '08 #2
>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.

Jun 27 '08 #3
On Jun 23, 8:34*pm, "Andrus" <kobrule...@hot.eewrote:
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.
Jun 27 '08 #4
>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.
Jun 27 '08 #5
On Jun 24, 10:52*pm, "Andrus" <kobrule...@hot.eewrote:
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.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by instruo | last post: by
3 posts views Thread by MBS | last post: by
3 posts views Thread by Stephen Gennard | last post: by
18 posts views Thread by Joe Fallon | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
19 posts views Thread by Simon Faulkner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.