Compiling code to dynamic method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrus

    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.

  • Pavel Minaev

    #2
    Re: Compiling code to dynamic method

    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.

    Comment

    • Andrus

      #3
      Re: Compiling code to dynamic method

      >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.

      Comment

      • Pavel Minaev

        #4
        Re: Compiling code to dynamic method

        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.

        Comment

        • Andrus

          #5
          Re: Compiling code to dynamic method

          >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.


          Comment

          • Pavel Minaev

            #6
            Re: Compiling code to dynamic method

            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.

            Comment

            Working...