473,238 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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 2574
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: instruo | last post by:
Hi all, Okay, here's something I'd like to do, but I'm not certain its possible. Thus far in playing with it, I haven't been able to get it going. I'm able to load and compile a .cs file using...
3
by: MBS | last post by:
As far as I know PHP code is always a script which is interpreted by the PHP interpreter, compiled to machine code, then executed by the CPU. Much like Java, one could say. (I am not talking...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
7
by: Clint Herron | last post by:
Howdy! I posted this question on CSharpCorner.com, but then realized I should probably post it on a more active newsgroup. This will be my only cross-post. I'm creating a game engine, and...
1
by: Mike Hutton | last post by:
I need some help. I am trying to set up our development environment so as to make life easy for my fellow developers (none of whom have used ASP.NET or VS.NET before). We are developing our...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
19
by: Simon Faulkner | last post by:
Pardon me if this has been done to death but I can't find a simple explanation. I love Python for it's ease and speed of development especially for the "Programming Challenged" like me but why...
0
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my case) and then use it with Python's ctypes. I can't...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.