473,786 Members | 2,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiling code to dynamic method

How to compile source code into dynamic method ?

System.Reflecti on.Emit.Dynamic Method 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 2607
On Jun 22, 1:00*am, "Andrus" <kobrule...@hot .eewrote:
How to compile source code into dynamic method ?

System.Reflecti on.Emit.Dynamic Method *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.Comp ile() 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.Comp ile() 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.JScri pt assembly. After that, the
usage is straightforward . Here's a simple JScript REPL in C#:

// Program.cs
using System;
using Microsoft.JScri pt;

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

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

try
{
var result = evaluator.Eval( script);
Console.WriteLi ne(result);
}
catch (JScriptExcepti on ex)
{
Console.WriteLi ne("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
1036
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 the CodeCompiler class. I know that portion works correctly because I can use the Invoke method of the MethodInfo object it returns and it triggers my dynamically loaded function. My problem shows up in +=ing my function onto an event. No...
3
2501
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 about the source code to PHP itself; rather, the code that one using the PHP language would write.) However, since the interpreter has to compile the PHP source prior to execution, it is being compiled anyway, so why not just compile it from the...
3
1312
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 below... --
7
10001
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 using CodeDOM for my scripting needs (I realize I could use yacc or something else, but I wanted to try using CodeDOM -- this is more of an exercise for me to learn this stuff). Well, I compile my in-game scripts just fine. See appendix A for my code
1
1732
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 intranet which will comprise basic content with a number of small data-driven ASP.NET applications. I need to keep things simple, so I need to avoid stuff which needs too much in-depth knowledge to work.
18
3164
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 question is what happens to the dynamically created assembly when the method is done running? Does GC take care of it? Or is it stuck in RAM until the ASP.Net process is recycled? This code executes pretty frequently (maybe 4 times per transaction) and...
0
9748
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. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
19
2195
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 hasn't someone written a compiler for Python? I guess it's not that simple eh? Simon
0
2279
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 say this will work for anyone else, but it worked for me... Using: http://ftp.mozilla.org/pub/mozilla.org/js/js-1.5.tar.gz First up:
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.