How the script library handles MethodArgument values
- Last UpdatedDec 13, 2023
- 2 minute read
The MethodArgument class in the script library stores argument values in one of two arrays called SimpleValue and StructValue.
-
If the actual argument value is a single value instead of an array, it is always contained in SimpleValue[0] or StructValue[0].
-
If the argument value is a simple (non-structured) type, that is, of type Value, it is stored in the SimpleValue array.
-
If the argument is a structured type (GenericStruct), it is stored in the StructValue array.
-
This also applies to the GenericField class. See Use complex (structured) data for more information.
Generally speaking, it is expected that you know the types of method arguments you are invoking. As an aid, however, the MethodArgument class also has two public properties (IsArray and IsStructure) that indicate whether the method argument is an array or a single value, and whether it is a simple type (Value) or a structured type (GenericStruct).
To illustrate dealing with MethodArgument data, consider the two lines of script code below:
multiplyResult = outputArgs[1][0]
Me.Result = multiplyResult.GetAsDouble();
This is equivalent to the following pseudo-code:
1. MethodArgument argument = outputArgs[1]
2. Value multiplyResult = argument[0]
3. double actualValue = multiplyResult.GetAsDouble();
4. Me.Result = actualValue;
Line 1 stores the (one and only) MethodArgument returned by the method call in argument (of type MethodArgument).
Line 2 calls an indexer in the MethodArgument class to return the first (and only, in this case) method argument value (of type Value) and stores it in multiplyResult. If the method argument value was a structured type, argument[0] would return a GenericStruct.
In Line 3, the actual value of the method call result of type double is obtained by converting the Value into a double by calling the GetAsDouble() method in the Value class.
Note: The first time the CallMethodAsync API is called, the script may log a timeout warning when run in synchronous mode,
as the API sets up the connection with the MethodProvider in the
Gateway Server. This warning can be ignored as the method call runs in a background
worker and returns the Method OutputArguments, on completion.