Un schéma d’appel C# vers JS trouvé sur Internet chaîne publique GetTimeByJs() { Type obj = Type.GetTypeFromProgID(« ScriptControl ») ; si (obj == null) return null ; objet ScriptControl = Activator.CreateInstance(obj) ; Obj. InvokeMember(« Language », BindingFlags.SetProperty, null, ScriptControl, new object[] { « JavaScript » }) ; chaîne js = « fonction time (a, b, msg){ var sum = a + b ; return new Date().getTime() + ' : ' + msg + ' = ' + somme } ; Obj. InvokeMember(« AddCode », BindingFlags.InvokeMethod, null, ScriptControl, nouvel objet[] { js }) ; Retour obj. InvokeMember(« Eval », BindingFlags.InvokeMethod, null, ScriptControl, new object[] { « time(1, 2, '1 + 2') » }). ToString() ; }
Le test est correct. temps (1, 2, '1 + 2'), où les paramètres passés sont (nombre 1, nombre 2, chaîne 1+2). Mais il y a une question, new object[] { « time(1, 2, '1 + 2') » }, si l’argument de la méthode JS est de type octet[], comment doit-il être écrit lors de l’appel ?
|