This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17689|Reply: 4

[New Issue] C# winform call js issue

[Copy link]
Posted on 2/18/2020 10:30:34 AM | | |
A C# call to JS scheme found on the Internet  
   public string GetTimeByJs()
        {
            Type obj = Type.GetTypeFromProgID("ScriptControl");
            if (obj == null) return null;
            object ScriptControl = Activator.CreateInstance(obj);
            obj. InvokeMember("Language", BindingFlags.SetProperty, null, ScriptControl, new object[] { "JavaScript" });
            string js = "function time(a, b, msg){ var sum = a + b; return new Date().getTime() + ': ' + msg + ' = ' + sum }";
            obj. InvokeMember("AddCode", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { js });
            return obj. InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { "time(1, 2, '1 + 2')" }). ToString();
        }   


The test is fine.
time(1, 2, '1 + 2'), where the parameters passed in are (number 1, number 2, string 1+2).
But there is a question, new object[] { "time(1, 2, '1 + 2')" }, if the argument of the JS method is of type byte[], how should it be written when calling?





Previous:SQLyog-12.0.9-0 with registration code
Next:Resource upload review
Posted on 2/18/2020 10:56:17 AM |
JavaScript data types can be divided into two categories:

primitive type: e.g. Undefined, Null, Boolean, Number, String
Reference type value, that is, object type: Object type, such as Object, Array, Function, Date



The arguments of JS methods do not have byte[], everything is object
Utilize Jurassic to execute js functions under .net
https://www.itsvse.com/thread-4303-1-1.html
(Source: Architect_Programmer)


 Landlord| Posted on 2/18/2020 2:10:18 PM |
Xiao Zhazha Posted on 2020-2-18 10:56
JavaScript data types can be divided into two categories:

primitive type : such as Undefined, Null, Boolean, Numb ...

Ao is like thisI don't know much about JS.
If
Suppose the time function parameter of js is an array
function time(Array ){....................}

Follow the last sentence of this C# function
return obj. InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { "time(????)" }). ToString();

I want to pass an Array   
var mycars = new Array();
mycars[0] = "a";
mycars[1] = "b";
mycars[2] = "c";

If you only call JS on this C# code implementation, you don't care about other code methods. That's in this C# code??? How should the parameters be written?
Posted on 2/18/2020 2:17:06 PM |
zncliving Posted on 2020-2-18 14:10
Ao: That's right, I don't know much about JS.
If
Suppose the time function parameter of js is an array

Well, I can only give you ideas, not help you write code

You still need to research the code yourself
 Landlord| Posted on 2/18/2020 2:35:32 PM |
Xiao Zhazha Posted on 2020-2-18 14:17
Well, I can only give you ideas, not help you write code

You still need to research the code yourself

Okay, I'm looking for information
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com