ClearScript is an open source library on the. NET platform for executing script code in C#and other. NET languages. It provides a convenient and secure way to integrate scripts with applications and allows applications to be exposed to scripts for higher levels of customization and interaction. This article will provide an in-depth introduction to the use and characteristics of ClearScript, including how to execute JavaScript scripts in C#, how to interact with scripts, and how to call C#methods.
installation and configuration
ClearScript can be installed through the NuGet Package Manager. To install ClearScript, open the NuGet Package Manager console in Visual Studio and run the following command:
Install-Package ClearScript
After the installation is complete, you also need to copy the runtimes directory under the ClearScript nuget package to the run directory, and then you can use the ClearScript library in your project.

Executing JavaScript scripts
To execute JavaScript scripts in C#, you need to create an instance of the JavaScript engine and pass the script to that instance. The following is a simple example that demonstrates how to execute a simple JavaScript program:
using var engine = new V8ScriptEngine();
engine.Execute("var a = 10; var b = 20; var c = a + b;");
var result = engine.Script.c;
Console.WriteLine(result); // 输出 30
In this example, we create a V8 ScriptEngine object called "engine" and call its Execute () method to execute some JavaScript code. In this case, we define three variables (a, b, and c), add them together, and store the result in variable c. We then retrieve the value of the variable c from the engine.Script object and output it to the console.
Interaction with scripts
When executing a JavaScript script, you can pass C#objects to the script so that the script can access them. To pass an object to a script, you need to add the object to the JavaScript engine using the AddHostObject () method. The following is a simple example that demonstrates how to pass a C#object to JavaScript:
/// <summary>
/// Person类需要为Public,V8引擎才能正常访问
/// </summary>
public class Person
{
public string? Name { get; set; }
public int Age { get; set; }
}
/// <summary>
/// JS与C#交互
/// </summary>
static void InteractionBetweenJsAndCsharp()
{
using var engine = new V8ScriptEngine();
var person = new Person { Name = "沙漠尽头的狼", Age = 18 };
engine.AddHostObject("person", person);
engine.Execute("var c = person.Name + ' 才 ' + person.Age + ' 岁呀?';");
var result = engine.Script.c;
Console.WriteLine(result); // 沙漠尽头的狼 才 18 岁呀?
}
在这个示例中,我们创建了一个名为“person”的 C# 对象, 注意Person的定义访问修饰符为public,并使用 AddHostObject() 方法将其添加到 JavaScript 引擎中。然后,我们执行一个 JavaScript 程序,该程序拼接person对象的属性组成一个JS变量,最后C#访问JS变量输出到控制台(尝试在JS中使用console.log输出未成功,有知道原因的朋友请留言告知)。
JS calls the C#method
In addition to passing C#objects to JavaScript, you can also call C#methods in JavaScript. To call the C#method in JavaScript, you need to create a class that contains the method and add the class to the JavaScript engine using the AddHostObject () method. The following is a simple example that demonstrates how to call the C#method in JavaScript:
/// <summary>
/// JS调用C#的方法
/// </summary>
static void JsCallCSharpMethod()
{
using var engine = new V8ScriptEngine();
var calculator = new Calculator();
engine.AddHostObject("calculator", calculator);
engine.Execute("var result = calculator.Add(15, 20)");
var result = engine.Script.result;
Console.WriteLine(result); // 35
}
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
In this example, we create a Calculator object called "calculator" and add it to the JavaScript engine using the AddHostObject () method. Then, we execute a program in JavaScript that calls the Add () method of the Calculator object and assigns the result to a JS variable. Finally, we get the variable value in C#and output it to the console.
Multithreaded use
ClearScript also supports the use of the JavaScript engine in multiple threads. To use the JavaScript engine in multiple threads, you need to create multiple instances of the JavaScript engine and use the respective threads to execute the script. The following is a simple example that demonstrates how to use the JavaScript engine in multiple threads:
using System.Threading.Tasks;
using Microsoft.ClearScript.V8;
var engine1 = new V8ScriptEngine();
var engine2 = new V8ScriptEngine();
Task.Run(() =>
{
engine1.Execute("var a = 'Hello from thread 1!'");
});
Task.Run(() =>
{
engine2.Execute("var b = 'Hello from thread 2!'");
});
In this example, we created two V8 ScriptEngine objects named "engine1" and "engine2" and executed two JavaScript programs in two different threads. These programs define JS variables.
It should be noted that when using a JavaScript engine in multiple threads, you should avoid accessing the same JavaScript engine instance at the same time to avoid thread safety issues.
summary
This article introduces the usage methods and characteristics of ClearScript, including how to execute JavaScript scripts in C#, how to interact with scripts, how to call C#methods, and multi-threaded use. ClearScript provides a convenient and safe way to integrate scripts with applications and allows applications to be exposed to scripts for higher levels of customization and interaction. By using ClearScript, you can add flexibility and extensibility to your application and implement dynamic script execution capabilities in your application.
-
- References **
- ClearScript Examples:https://microsoft.github.io/ClearScript/Examples/Examples.html