admin管理员组

文章数量:1356696

I want to run Javascript within C# and pass variables between C# and Javascript.

It seems ClearScript is the current stable way to do this.

I have a JavaScript function that looks something like this:

var b = a[0];
var c = a[1];
var d = a[2];
var e = a[3];
rtnstr = "{errmsg: 'calculation never ran'}";

calculation()

function calculation() {
   rtnstr = "{ one:'" + a+b "', " two:'" + c+d + "'}";
}

How can I call that in ClearScript passing in the a array and fetching back the rtnstr string.

I found this URL: , which shows how to retrieve an array of integers; I need it to retrieve one String.

I also need to know how to pass in variables; the example does not show that.

I want to run Javascript within C# and pass variables between C# and Javascript.

It seems ClearScript is the current stable way to do this.

I have a JavaScript function that looks something like this:

var b = a[0];
var c = a[1];
var d = a[2];
var e = a[3];
rtnstr = "{errmsg: 'calculation never ran'}";

calculation()

function calculation() {
   rtnstr = "{ one:'" + a+b "', " two:'" + c+d + "'}";
}

How can I call that in ClearScript passing in the a array and fetching back the rtnstr string.

I found this URL: https://clearscript.codeplex., which shows how to retrieve an array of integers; I need it to retrieve one String.

I also need to know how to pass in variables; the example does not show that.

Share Improve this question edited Mar 10, 2016 at 3:01 Be Kind To New Users asked Mar 10, 2016 at 2:50 Be Kind To New UsersBe Kind To New Users 10.1k16 gold badges87 silver badges133 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I don't know if you still need this, but I stumbled upon your question and gave it a shot.

First add a JS file to your solution:

Add a javascript file to your solution.

Then add your javascript to the javascript file:

var myArray = [0,1,2,3];
var a = myArray[0];
var b = myArray[1];
var c = myArray[2];
var d = myArray[3];
var rtnstr = "{errmsg: 'calculation never ran'}";

function calculation() {
   var one = a + b;
   var two = c + d;
   rtnstr = "{ one:'" + one  + "', two:'" + two + "'}";
   return rtnstr;
}

In your C# code then do something like the following:

using System;
using System.IO;
using Microsoft.ClearScript.V8;

namespace ClearScriptConsole
{
    class Program
    {
        static void Main(string[] args) {

            V8ScriptEngine _v8Engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);

            string jsFilePath = @"..\..\HelloWorld.js";
            string jsContents = File.ReadAllText(jsFilePath);

            _v8Engine.Execute(jsContents);

            var returnedVal = _v8Engine.Script.calculation();
            Console.WriteLine(returnedVal);
        }
    }
}

And the returned value gave me the following:

The result of the calculation displayed in a console window.

Cheers

I am way late and a dollar short, but here is another solution. I was looking for a quick answer before stumbling across it Intellisense. It sounds like you are looking for ClearScript's "Invoke" method:

Given that you might have a JavaScript function like:

function calculation(a) {
    var b = a[0];
    var c = a[1];
    var d = a[2];
    var e = a[3];

    return `{{ one: ${(b + c)}, two: ${(d + e)} }}`;
};

That you wish to call from C# with an array of ints:

var rtnstr = engine.Invoke("calculation", new int[] { 1, 2, 3, 4, 5 });

Possible bug in ClearScript 5.6: It does not seem to like function variables. I originally tried to declare the function as:

const calculation = (a) => {
    var b = a[0];
    var c = a[1];
    var d = a[2];
    var e = a[3];

    return `{{ one: ${(b + c)}, two: ${(d + e)} }}`;
};

But I received an exception "Method or property not found". Declaring as a plain old function in the global scope works though.

Note that I haven't worked on this and am trying to connect the dots using documentation.

using Microsoft.ClearScript.V8;

public static void Main(string[] args)
{
    var engine = new V8ScriptEngine();
    engine.AddHostObject("a", args);    // it looks like a is the name of the variable that wil be used by script to refer to host object which is args in this case

    engine.Execute(".... your javascript code ....");
    var returnStr = engine.Script.rtnstr;
}   

本文标签: Passing variables to Javascript using ClearScriptStack Overflow