comunication between JScript and C#

B

bowser

Hello,
I have a problem of communication between JScript and C#. I must say
that I'm new to both.
In a JS file I have to call a C# function.
In particular I have:

public function Eval(expr : String) : String
{
return eval(expr);
}

And I want to call:

Eval("MyNS.myFunction()");

where myFunction is declared in a C# file, under the namespace MyNS,
and returns a string.

Is it possible to do such a thing without to consider COM objects
(which I've heard to be used in similar problems)?

Note that the .js file is generated dinamically by a CodeDomProvider in
C#.
It is compiled by adding the reference of the .exe file, which contains
myfunction():
parameters.ReferencedAssemblies.Add("MyApp.exe");
So the call Eval("MyNS.myFunction()"); will be done with Reflection

One more question: is there another way to use the jscript function
eval in C#? Maybe compiling a .js module (not dinamically and not using
reflection) that can be used by a C# module?

Thank you.

Alessandro
 
M

Michael Nemtsev

Hello bowser,

Several ways
1) AJAX. There are several implementaion of it - from Microsoft and other
companies
2) Client Callback features: http://dotnetjunkies.com/Tutorial/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik

Google to find out more examples
For example see there http://www.codeproject.com/aspnet/AlvaroRemoteScripting.asp

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

b> Hello,
b> I have a problem of communication between JScript and C#. I must say
b> that I'm new to both.
b> In a JS file I have to call a C# function.
b> In particular I have:
b> public function Eval(expr : String) : String
b> {
b> return eval(expr);
b> }
b> And I want to call:
b>
b> Eval("MyNS.myFunction()");
b>
b> where myFunction is declared in a C# file, under the namespace MyNS,
b> and returns a string.
b>
b> Is it possible to do such a thing without to consider COM objects
b> (which I've heard to be used in similar problems)?
b>
b> Note that the .js file is generated dinamically by a CodeDomProvider
b> in
b> C#.
b> It is compiled by adding the reference of the .exe file, which
b> contains
b> myfunction():
b> parameters.ReferencedAssemblies.Add("MyApp.exe");
b> So the call Eval("MyNS.myFunction()"); will be done with Reflection
b> One more question: is there another way to use the jscript function
b> eval in C#? Maybe compiling a .js module (not dinamically and not
b> using reflection) that can be used by a C# module?
b>
b> Thank you.
b>
b> Alessandro
b>
 
B

Ben Voigt

bowser said:
Hello,
I have a problem of communication between JScript and C#. I must say
that I'm new to both.
In a JS file I have to call a C# function.
In particular I have:

public function Eval(expr : String) : String
{
return eval(expr);
}

That's not JavaScript/JScript/ECMAScript/whaddevayacallit... are you using
JScript.NET? JScript.NET should be able to call directly into any .NET
Assembly.
And I want to call:

Eval("MyNS.myFunction()");

where myFunction is declared in a C# file, under the namespace MyNS,
and returns a string.

Err, C# functions can't be loose in a namespace. You need a class name in
there somewhere.
Is it possible to do such a thing without to consider COM objects
(which I've heard to be used in similar problems)?

Note that the .js file is generated dinamically by a CodeDomProvider in
C#.
It is compiled by adding the reference of the .exe file, which contains
myfunction():
parameters.ReferencedAssemblies.Add("MyApp.exe");
So the call Eval("MyNS.myFunction()"); will be done with Reflection

If you have a metadata reference, why use Eval at all? With a reference the
JScript.NET compiler knows about MyNS and you can directly do:
var result = MyNS.MyClass.myFunction();
with no eval call
One more question: is there another way to use the jscript function
eval in C#? Maybe compiling a .js module (not dinamically and not using
reflection) that can be used by a C# module?

There's Microsoft.JScript.Eval.JScriptEvaluate method, but documentation is
really bad. You'd have to disassemble it to find out if it's useful to you.
 
W

Willy Denoyette [MVP]

bowser said:
Hello,
I have a problem of communication between JScript and C#. I must say
that I'm new to both.
In a JS file I have to call a C# function.
In particular I have:

public function Eval(expr : String) : String
{
return eval(expr);
}

And I want to call:

Eval("MyNS.myFunction()");

where myFunction is declared in a C# file, under the namespace MyNS,
and returns a string.

Is it possible to do such a thing without to consider COM objects
(which I've heard to be used in similar problems)?

Note that the .js file is generated dinamically by a CodeDomProvider in
C#.
It is compiled by adding the reference of the .exe file, which contains
myfunction():
parameters.ReferencedAssemblies.Add("MyApp.exe");
So the call Eval("MyNS.myFunction()"); will be done with Reflection

One more question: is there another way to use the jscript function
eval in C#? Maybe compiling a .js module (not dinamically and not using
reflection) that can be used by a C# module?

Thank you.

Alessandro


Sure it's possible, both are managed code languages so interop is just a matter of importing
and referencing the correct assembly.

Here is a small sample.
1 is a CS file with a simple calls and method that returns a string.
2 is a 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);
}
};

Hope this helps you to get started.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top