Click here to Skip to main content
14,974,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I recently migrated from StackOverflow to here cause I got temp banned because my "question wasn’t received well by the community" and wasn’t fully answered.

Anyway, I’m trying to use JavaScript as an external way to code sub programs for my .NET C# application. But how do you call C# methods from JavaScript?

I am not trying to code server or client things, I just need:

A method to call and run the JavaScript Code

Detect when it calls a C# method (included in the method that the JavaScript code was called from)

What I have tried:

I have tried using ClearScript by Microsoft but it's documentation confuses me.

And I have also tried asking StackOverflow but that didn’t go so well
Posted
Updated 3-May-21 10:59am
v2
Comments
Gerry Schmitz 3-May-21 13:17pm
   
If the answers isn't out there, or there is no feasible answer, repeating the question just becomes annoying.

There's a concept called: accepted standards, practices and procedures.

You haven't said anything about "why", but I'm pretty certain whatever it is (going out on a limb here), that it can be done another way.

Most question sound ridiculous when there is no "why".
gggustafson 3-May-21 14:50pm
   
From your reputation, I take it that you are a relatively new C# programmer. As a programmer in both C# and JavaScript, I strongly suggest that you use a C# method in place of trying to execute JavaScript from your C# code.

I have found that JavaScript and C# can be easily rewritten from one to the other.

Another thought, has the architecture you are considering been validated? You want to do something that's very complex (inter-language interfaces are quite messy and error-prone) when a simpler solution (rewrite the JavaScript) exists.

Good luck
VinnyRulesGaming 3-May-21 14:52pm
   
I am trying to use JavaScript as a way for users to develop sub applications for my software. Basically, JavaScript will do the math and call my methods from within the compiled C#
gggustafson 3-May-21 15:17pm
   
I disagree with your design. Rather than allow a user to write JavaScript (very likely a source of errors), you should decide what your users are allowed to do and then develop a GUI that lets them pick and choose. You are opening a can of worms. I needed to do something like what you wanted to do (1976) and had to spend an inordinate amount of time implementing a NEW LANGUAGE to do it. Not for the squeamish. There are .NET API entries that let you do what you want, but they are beyond the scope of "Quick Answers".

1 solution

Unless you want to write your own Javascript interpreter and execution engine, you would have to use a 3rd party library to do something like this, like Jint[^].

But, you're at the mercy of those people who write the library. Not every feature of every ECMAScript standard is going to be implemented and some things may not work to standard.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900