microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.77k stars 148 forks source link

Javascript code import C# assembly (dll,exe) #477

Closed stone89son closed 1 year ago

stone89son commented 1 year ago

hi everyone. If user can import C# assembly (dll,exe) from javascript code same node module, I think very useful. in the case, Is there a way to that?

ClearScriptLib commented 1 year ago

Hi @stone89son,

You can't load .NET assemblies via import or require, but ExtendedHostFunctions.lib provides something similar:

engine.AddHostObject("host", new ExtendedHostFunctions());
engine.AddHostType(typeof(Console));
engine.Execute(@"
    const clr = host.lib('mscorlib', 'System', 'System.Core');
    let array = clr.System.Linq.Enumerable.Range(0, 5).ToArray();
    Console.WriteLine(Array.from(array).toString()); // output: 0,1,2,3,4
");

Good luck!

ClearScriptLib commented 1 year ago

Please reopen this issue if you have additional thoughts or questions about this topic. Thank you!