5

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.

6
  • is this Javascript or JScript ? Is this with Asp.net ? Commented Feb 27, 2012 at 19:55
  • @MicahArmantrout: msdn.microsoft.com/en-us/library/xkx7dfw1.aspx Commented Feb 27, 2012 at 19:55
  • Are you trying to call the c# function from javascript Commented Feb 27, 2012 at 20:02
  • Are you trying to do this new JScriptCodeProvider().CreateCompiler(); as stated here: odetocode.com/code/80.aspx Commented Feb 27, 2012 at 20:36
  • @COLDTOLD Yes,because I do not like writing similar functions in javascript again. Commented Feb 28, 2012 at 4:50

2 Answers 2

9

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);
    }
};
Sign up to request clarification or add additional context in comments.

Comments

0

What you need to to is to convert your JScript.NET code to C#. Something like this.

1 Comment

There is a new version of JScript.NET in Visual Studio 2010: msdn.microsoft.com/en-us/library/xkx7dfw1.aspx. I'm guessing there's a way to call C# code from JScript.NET, provided you compile your C# code into a .NET DLL and reference it from JScript.NET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.