Skip to content
CheshireCaat edited this page Feb 8, 2020 · 15 revisions

Default 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 from Newtonsoft.Json.
  • JavaScript arrays will be represented as JArray type from Newtonsoft.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.

Generic types

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<SelectedType>
  • JArray will be converted to specified type using JArray.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.

Example

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);
Clone this wiki locally