Working with Dynamic Code Execution Functions
You can use the Dynamic Code Execution functions to dynamically execute .NET (C# and Visual Basic) code. The dynamic code execution libraries compile and execute the C# and Visual Basic code when RT Client runs. See Dynamic Code Manager Library Objects for details on the functions.
The following dlls in RT Designer are used when you execute your Dynamic Code Execution functions:
Direct.Scripting.dll
Direct.RemoteLoader.dll
To use the library, basic knowledge of the C# and/or VB languages is required.
Direct.Scripting.dll
This library provides the DynamicCodeManager business type. Create an instance of the DynamicCodeManager library and configure the following properties.
Name | Type | Description |
---|---|---|
ScriptingLanguage | Text |
Language of the code to be executed dynamically. Supported values: Csharp/csharp for C# code VB/vb for Visual Basic code |
ReferencedAssemblies | List of Text |
The assemblies that are referred to from your code. Format to add the referenced assemblies:
If the dll is a part of your framework, just specify the namespace of the class used in your code. Examples: With dll name: Without dll name: If you have referred to any third-party library class, or any existing RT Designer library from your code, you need to specify the full path to the library along with the namespace. Example:
Note: You MUST copy your third-party dlls to the location where RT Designer is installed. |
SaveSourceCode | Boolean | If set to True, RT Designer returns the code APA generates to execute your CSharp or Visual Basic code. This property is useful if you want to resolve any compilation errors in your code. |
GeneratedSourceCode | Text | If you set the SaveSourceCode property value to True, then the GeneratedSourceCode property holds the generated source code along with your C# or Visual Basic code. Use this property to check your code for any compilation errors. |
IsError | Boolean | APA sets this property value to True if any error is detected during the code compilation or code execution. |
ErrorMsg |
Text |
Displays the detailed description of the code compilation or execution error. |
Direct.RemoteLoader.dll
This is a helper library used internally by the Direct.Scripting.dll functions.
Important Notes
APA supports the following parameter types only:
String
Integer/number
Boolean
Decimal
Limitations
The string return type is not supported.
A variable of dynamic type is not supported.
APA cannot dynamically execute the code if the code:
Contains variables defined as dynamic.
Calls a third-party dll that has a dynamic variable.
Using the Dynamic Code Executions Functions
To use the dynamic code execution functions:
1. | Code the required methods in C# or VB and compile a dll. |
2. | Copy the dll to a location that you will reference in your project and into the Real-Time Designer installation folder. |
3. | Create a Real-Time Designer project. |
4. | Create an instance of the DynamicCodeManager library and configure the properties (ScriptingLanguage, ReferencedAssemblies, SaveSourceCode and GeneratedSourceCode). |
5. | Use the Execute Method function to execute the required method with a list of parameters (with a corresponding list of the types of the parameters). |
6. | For example, in this snippet of code you can execute the IsAlphaNumeric method with the C#input business entity that will be used as the parameter passed into the method, and string as the type of the parameter. |
Dynamic Code Sample
A sample project can be downloaded here.
The Quick callout in the sample includes several examples:
Sets the language to C#.
Executes a C# method.
Sets the language to VB.
Executes a VB method.
Executes a method in a referenced assembly.
To test the dynamic code sample:
1. | Copy the myLibrary.dll from the sample into C:\DynamicCode and into the Real-Time Designer installation folder. |
2. | Open and run the sample DynamicCode project. |
3. | Open the Quick callout. |
4. | To set the language to C# code, click SetLanguageAsC#. |
5. | Open Monitor. |
6. | In dynamicCode, in C#input enter test123. |
7. | In the Quick callout, click ExecuteCSharpCode. This executes an IsAlphaNumeric method that checks if the C#input value is alphanumeric. The C#result is now True. |
8. | The IsAlphaNumeric function can be viewed in the Quick callout Action Link window. |
9. | In Monitor, view the generated code in GeneratedSourceCode. |
10. | IsError is False indicating that the code was executed without any errors (no ErrorMsg was generated), the SaveSourceCode is set to True, and the ScriptingLanguage is CSharp. |
11. | Similarly, you can check the VB code execution which creates a factorial value, for example, enter 5 in VBinput and when you run the function 150 is returned in VBresult. |
12. | To test the final action, ensure that you have copies the myLibrary.dll from the sample into C:\DynamicCode and in the Real-Time Designer installation folder. |
13. | In the Quick callout, click ExecuteC#CodeWith Libraries. This executes the mul method that multiplies two integers, a and b, where a is 5 and b is 7. The function can be viewed in the Quick callout Action Link window. |
14. | The result is 35. |
15. | In Monitor, view the generated code in GeneratedSourceCode. |