PC Review


Reply
Thread Tools Rate Thread

Compiling code to dynamic method

 
 
Andrus
Guest
Posts: n/a
 
      21st Jun 2008
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.

 
Reply With Quote
 
 
 
 
Pavel Minaev
Guest
Posts: n/a
 
      22nd Jun 2008
On Jun 22, 1:00*am, "Andrus" <kobrule...@hot.ee> wrote:
> 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.
 
Reply With Quote
 
Andrus
Guest
Posts: n/a
 
      23rd Jun 2008
>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.

 
Reply With Quote
 
Pavel Minaev
Guest
Posts: n/a
 
      24th Jun 2008
On Jun 23, 8:34*pm, "Andrus" <kobrule...@hot.ee> wrote:

> 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.
 
Reply With Quote
 
Andrus
Guest
Posts: n/a
 
      24th Jun 2008
>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.


 
Reply With Quote
 
Pavel Minaev
Guest
Posts: n/a
 
      24th Jun 2008
On Jun 24, 10:52*pm, "Andrus" <kobrule...@hot.ee> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: is there a method for dynamic compilation of aspx.vb (or .cs) code off the web? Patrice Microsoft ASP .NET 0 2nd Oct 2009 11:00 PM
Re: is there a method for dynamic compilation of aspx.vb (or .cs)code off the web? bruce barker Microsoft C# .NET 0 2nd Oct 2009 02:20 AM
Re: is there a method for dynamic compilation of aspx.vb (or .cs)code off the web? bruce barker Microsoft VB .NET 0 2nd Oct 2009 02:20 AM
Dynamic code compiling and events =?Utf-8?B?aW5zdHJ1bw==?= Microsoft C# .NET 2 11th Mar 2005 02:50 AM
Dynamic code compiling and events =?Utf-8?B?aW5zdHJ1bw==?= Microsoft Dot NET 0 8th Mar 2005 02:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:02 PM.