Un esquema de llamada a JS en C# encontrado en Internet cadena pública GetTimeByJs() { Tipo obj = Tipo.ObTypeFromProgID("ScriptControl"); si (obj == null) return null; objeto ScriptControl = Activator.CreateInstance(obj); Obj. InvokeMember("Language", BindingFlags.SetProperty, null, ScriptControl, new object[] { "JavaScript" }); cadena js = "función tiempo(a, b, msg){ var sum = a + b; return new Date().getTime() + ': ' + msg + ' = ' + sum }"; Obj. InvokeMember("AddCode", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { js }); Regresar obj. InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { "time(1, 2, '1 + 2')" }). ToString(); }
La prueba está bien. tiempo (1, 2, '1 + 2'), donde los parámetros pasados son (número 1, número 2, cadena 1+2). Pero hay una pregunta, new object[] { "time(1, 2, '1 + 2')" }, si el argumento del método JS es de tipo byte[], ¿cómo debería escribirse al llamar?
|