I'm trying out Jint and want to implement the fetch api to use from js. The second argument is a nested js object that is passed in:
fetch('https://example.com', {
method: 'post',
headers : {
authorization : 'xyz'
}
});
What is the signature for the C# function to pass in? I tried
private static string Fetch(string uri, object init)
First and saw Jint passed in an ExpandoObject, I made this:
private static string Fetch(string uri, ExpandoObject init)
But Jint use the JsValue and ObjectInstance object internally right? These also have better sematics to work out is what you got passed in was an object or array etc, but this throws an error:
private static string Fetch(string uri, ObjectInstance init)
: 'No valid constructors found'
What would be the recommended way to pass these objects to C#? I want to have as little friction between the js and C# side of the app as I'll be making quite a few calls back and forth (which is why I'm looking at Jint coming from ClearScript)
Some choices:
JsValue
The object is passed as is.
You can't use any other derived type because jint only checks
JsValue
. To useObjectInstance
, cast it on C# side.dynamic
This is the equivalent of
ExpandoObject
.Jint will convert an
ObjectInstance
to anExpandoObject
first if the parameter type is other thanJsValue
.IDictionary<string,object>
or any other generic interface thatExpandoObject
implementedSame as
dynamic
.A custom type
that the input object is able to convert to.You could define the object as class or struct, and define the entries as field or property. The name of the member can be capitalized. Notice that this way uses reflection, so it's not effcient.
For instance, you can define it like this:
object
Always works.