Skip to content
CheshireCaat edited this page Feb 13, 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 or additional methods 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.

Examples


In this example you can see two calls for one function - with and without generic type.

// Run function and wait for result with specified type.
// genericResult is int here.
var genericResult = await client.RunFunction("Add", new Params
{
    {"X", 5},
    {"Y", 5}
}).GetTask<int>();

// Run function and wait for result with default type.
// defaultResult is dynamic here.
var defaultResult = await client.RunFunction("Add", new Params
{
    {"X", 5},
    {"Y", 5}
}).GetTask();

// It prints 'true'.
Console.WriteLine(genericResult == defaultResult);

Working with arrays using string[] and JArray types as an example.

 // This variable will be of type string[].
var genericResults = await client.RunFunction("GoogleSearch", new Params
{
    ["Query"] = "cats"
}).GetTask<string[]>();

// This variable will be of type JArray.
var defaultResults = await client.RunFunction("GoogleSearch", new Params
{
    ["Query"] = "cats"
}).GetTask();

// Print links from string array.
foreach (var link in genericResults) Console.WriteLine($"[generic] - {link}");

// Print links from JArray.
foreach (var link in defaultResults) Console.WriteLine($"[default] - {link}");

Working with objects using Dictionary<string, dynamic> and JObject types as an example.

// This variable will be of type Dictionary<string, object>.
var genericResults = await client.RunFunction("TestObject", Params.Empty)
    .GetTask<Dictionary<string, dynamic>>();

// This variable will be of type JObject.
var defaultResults = await client.RunFunction("TestObject", Params.Empty)
    .GetTask();

// Print items from Dictionary.
foreach (var item in genericResults)
{
    // Item value is JObject here.
    var name = item.Value.name;
    var id = item.Value.id;
    Console.WriteLine($"[generic] - {item.Key} - {name}|{id}");
}

// Print items from JObject.
foreach (var item in (JToken) defaultResults)
{
    var data = (JProperty) item;
    var name = data.Value["name"];
    var id = data.Value["id"];
    Console.WriteLine($"[default] - {data.Name} - {name}|{id}");
}
Clone this wiki locally