DLR and Javascript interpretation in c# 4?
See the question and my original answer on StackOverflowIf you're looking for a 1:1 match between how your javascript is executed in .NET and how it would be executed in a windows program such as Internet Explorer, there is a way to reuse the Windows script engines (not only Jscript but also VBScript or any other ActiveX Scripting language) that's described here on SO: parse and execute JS by C#
This is how your code could be implemented using this technique:
var script = @"
function show( )
{
return parseInt('123asd'); //in js it's 123
}"; // Note I have removed the return as it's not needed here
using (ScriptEngine engine = new ScriptEngine("jscript"))
{
ParsedScript parsed = engine.Parse(script);
Console.WriteLine(parsed.CallMethod("show"));
}
}
This will output 123
as expected. Note I think the original code could be improved with the new dynamic
C# keyword, so we could probably write Console.WriteLine(parsed.show())
directly.