0

I am working on a project that uses Javascript.net (formerly Noesis Javascript .net) as a scripting engine.

How do you add a C# method to the global context in Javascript? For example, say I have in C#:

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

I want to be able to call in 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));

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

In my case I wanted to expose a static C# method in the global object. So I used _context.SetParameter("myfunc", new Action<string>(TheFunctionInCSharp)); which worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.