Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: How to pass short int from script to C# methods

$
0
0
Which method should be called by the JavaScript expression thing.Foo(123)?
Ideally the engine should try to find Foo(int arg); then Foo(short arg), Foo(byte arg), Foo(sbyte), till it finds a match. I guess C/C++ uses this kind of promotion chain.
.NET fields and (normal) properties can't be generic or overloaded, so there's no ambiguity. The type of a field or property is always known, so ClearScript can always make an attempt to convert the argument to the correct type.
Unfortunately, the above sample code fails when you change the data type from "short" to "float":
public float MyValue;
void ClearScriptTest2(object sender, EventArgs e) {
    using (var engine = new V8ScriptEngine()) {
        engine.AddHostObject("Me", this);
        engine.Execute("Me.MyValue = 123.45;");
        MessageBox.Show(MyValue.ToString());
    }
}

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>