简体   繁体   English

在 Javascript.net 中添加全局方法

[英]Adding a global method in Javascript.net

I am working on a project that uses Javascript.net (formerly Noesis Javascript .net) as a scripting engine.我正在开发一个使用 Javascript.net(以前的 Noesis Javascript .net)作为脚本引擎的项目。

How do you add a C# method to the global context in Javascript?如何将 C# 方法添加到 Javascript 的全局上下文中? For example, say I have in C#:例如,假设我在 C# 中有:

public int Foo(int bar)
{
    return bar * 2;
}

I want to be able to call in Javascript:我希望能够调用 Javascript:

var x = 5;
var y = Foo(x); // y is now 10;

In other libraries such as Jurassic this is possible:在侏罗纪等其他图书馆中,这是可能的:

 engine.SetGlobalFunction("Foo", new Func<int, int>((a) => a*2));

This example comes from the unit tests:这个例子来自单元测试:

_context.SetParameter("f", new Func<string,string>((s) => s.ToUpper()));
_context.Run("f('Noesis') == 'NOESIS'").Should().BeOfType<bool>().Which.Should().BeTrue();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM