PC Review


Reply
Thread Tools Rate Thread

comunication between JScript and C#

 
 
bowser
Guest
Posts: n/a
 
      11th Jan 2007
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

 
Reply With Quote
 
 
 
 
Michael Nemtsev
Guest
Posts: n/a
 
      11th Jan 2007
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/E8...0EECF13D7.dcik

Google to find out more examples
For example see there http://www.codeproject.com/aspnet/Al...eScripting.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>


 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      11th Jan 2007

"bowser" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

>
> Thank you.
>
> Alessandro
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      11th Jan 2007
"bowser" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is Server-Side Jscript (not Jscript.NET)? Maxwell2006 Microsoft ASP .NET 5 7th Mar 2006 05:28 AM
Porting from JScript to JScript.Net - compiler error Jon Maz Microsoft ASP .NET 4 9th Sep 2004 11:24 AM
comunication Pedro Microsoft Dot NET Framework 1 30th Dec 2003 06:03 PM
Re: Forms comunication Eliyahu Goldin Microsoft C# .NET 0 2nd Sep 2003 09:00 AM
Re: Forms comunication Justin Rogers Microsoft C# .NET 0 1st Sep 2003 10:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:21 AM.