Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call C# from JScript.net?

Tags:

c#

jscript.net

I read that, for the newest Java, Javascript on Java can call or import java packages easily. In the newest .NET, can JScript.net call C# functions easily?

For details, I am asking not about compiled JScript.net code, but about non-compiled JScript.net string code which is run on the script engine.

like image 529
seven_swodniw Avatar asked Feb 27 '12 19:02

seven_swodniw


People also ask

How do you call C in C++?

Just declare the C++ function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code: extern "C" void f(int);


1 Answers

Here is an example:

1) CS file with a simple calls and method that returns a string. 2) js file that calls the CS method using eval.

// cstest.cs - compile as library

using System;
namespace MyNamespace
{
    public class Foo
    {
        public string Bar()
        {
            return "Hello JS";
        }
    }
}

// test.js - compile as exe // add a reference to cstest.dll // command line compile jsc /t:exe /r:cstest.dll test.js

import MyNamespace;

var o : JSApp = new JSApp();
o.DoEval();

class JSApp
{
    function DoEval()
    {
        var f : Foo;
        var s : String
       eval("f = new Foo;");
       eval("s = f.Bar();"); // call Foo.Bar
       print(s);
    }
};
like image 192
Dmitry Savy Avatar answered Sep 25 '22 18:09

Dmitry Savy