Skip to main content

Introduction to C# data types in Frends

Insight to usage of dynamic and other data types in Frends.

Ossi Galkin avatar
Written by Ossi Galkin
Updated over a year ago

Frends Processes builds on C# and .NET, so data types use C# data types. However, especially dynamic and object data types might raise some confusion. 

Dynamic and object types are used in Frends to enable usage of the dot notation (like #result.body.foo.bar) commonly used in Frends. Technically DynamicMetaObject Class used by dynamic class enables digging inside data to finding methods used as a part in the path in dot notation.

Multiple internal Frends components and Process Elements handle data as JTokens (JArray, JObject, JProperty, JValue are JTokens), but in fact, most JTokens in Frends Processes are generic objects. Otherwise you wouldn't be able to access a JToken with a dot notation, but instead, you would have to access it with a ["key"] notation. Therefore, for example, JSON.ConvertJsonStringToJToken returns JToken as a generic object.
It should be noted that the third way to use the SelectToken method with a JsonPath e.g. SelectToken("$.whatever"). Usually the dot notation is used when working with Frends, but clarity between these styles is debatable. 

Two Process Elements that also often cause some confusion as they return a dynamic object are Code Elements and Subprocess. When a Code Element does not specify the type of the variable, it is dynamic. The result received from calling a Subprocess will be a dynamic object. 

The unfortunate consequence of using dynamic is that extension methods can not be called directly like usual, because extension methods are expanded during compilation and this requires the type to be known, which is not the case with dynamic objects. These errors will show messages like 'something' does not contain a definition for 'something'. They can be resolved the same way as any other situation where data is for some reason converted to a wrong C# data type. It has to cast to right one. 

These errors can be for example:

'object' does not contain a definition for 'ToArray'
'System.Array' does not contain a definition for 'Contains'
'System.Collections.Generic.Dictionary<string,string>.KeyCollection' does not contain a definition for 'First'  

And as noted earlier they arise from the fact that something is dynamic and they lack generic or original typing during compile time and thus extension methods, e.g. LINQ extension methods,  can not be accessed,

Solutions is to convert data to original data type or something suitable, using code such as:  

(string)#var.i["name"]).Split('\\').First()
(#env.clients as object[]).ToList().Contains()

The next article is Example on Parse.​

Did this answer your question?