-
Notifications
You must be signed in to change notification settings - Fork 11
Types
When calling functions and sending messages, you need to keep in mind what types the result may have. Since BAS uses JS as its primary language, some types may not be known in advance. Therefore, methods for sending messages and run functions by default work through dynamic
.
The main thing you need to know in this case:
- JavaScript objects will be represented as
JObject
type fromNewtonsoft.Json
. - JavaScript arrays will be represented as
JArray
type fromNewtonsoft.Json
.
Other types like strings, numbers, booleans will be converted to built-in .NET types.
Thus, you can, for example, use type comparisons with the as
and is
keywords and implement your own processing of the results.
Some methods in library like RunFunction
in IBasThread
and BasRemoteClient
contains overloads with support for generic types. When specifying a type, the following conversions will be performed:
JObject
will be converted to specified type using
JObject.ToObject```
-
JArray
will be converted to specified type usingJArray.ToObject<SelectedType>
Other types will be converter using System.Converter.ChangeType
.
But be careful, this does not save you from exceptions, so use these methods wisely.
In this example you can see two calls for one function - with and without generic type.
// Run function with specified type.
var genericResult = await client.RunFunction<int>("Add", new Params
{
{"X", 5},
{"Y", 5}
});
// Run function by default.
var defaultResult = await client.RunFunction("Add", new Params
{
{"X", 5},
{"Y", 5}
});
// It prints 'true'.
Console.WriteLine(genericResult == defaultResult);