Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
How do I return a value from a C# function using jint?

I'm currently using Jint (https://github.com/sebastienros/jint) to process JavaScript.

I would like to be able to use a custom function in JavaScript that will execute a function created in C# and then return a value to the JavaScript.

For example if the JavaScript was:

var x = MultiplyByTwo(100);

And in my C# I had:

private static int MultiplyByTwo(int obj)
{
    return obj * 2;
}

Then the above would give the variable x the value of 200.

According to the Documentation there is the following option:

var engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine));

engine.Execute(@"
    function hello() { 
        log('Hello World');
    };
 
    hello();
");

I can replace the "Console.Writeline" with a function name but it will only accept a void function. In other words, this can call a C# function from JS but not return a value.

Does an option like what I am looking for exist in Jint?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can certainly reference a class, which can involve setting properties, and methods can return values. For example:

class Program
{
    static void Main(string[] args)
    {
        var myobj = new MyClass();
        var engine = new Engine()
            .SetValue("log", new Action<object>(Console.WriteLine))
            .SetValue("myobj", myobj);

        engine.Execute(@"
        function hello() { 
            myobj.One = 'asdf';
            var x = myobj.TwoTimes(4);
            log(x); // logs 8
        };
    
        hello();
    ");
        Console.WriteLine($"{myobj.One}, {myobj.Two}");
        // output => "asdf 8"
    }

}

public class MyClass
{
    public string One { get; set; }
    public int Two { get; set; }
    public int TwoTimes(int value)
    {
        Two = 2 * value;
        return Two;
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

As mentioned in some comments, use

SetValue("multiplyByTwo", new Func<int>(MultiplyByTwo))
over 4 years ago · Santiago Trujillo Relatório

0

Here is a fully functional example. Casting Console.WriteLine is required since there are multiple overloads and we need to tell the compiler which one to use:

using System;
using Jint;

var engine = new Engine()
    .SetValue("multiplyByTwo", MultiplyByTwo)
    .SetValue("log", (Action<string>)Console.WriteLine)
    ;
    
engine.Execute(@"
    var x = multiplyByTwo(3);
    log(x);
");

static int MultiplyByTwo(int obj)
{
    return obj * 2;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda