CSharp Documentation
Version: 4.0.0
Retrieved: 2025-10-09 15:15:48
CSharp Code Module
The module allows users to write C# scripts in the UI. The code is compiled at runtime and allows users to manipulate the data sent to the module or create new data to be sent to the next module(s).
This module is designed to be located in the middle of a flow.
Settings
The Main property will hold the C# code to execute when the module receives a message.
The CrosserTech.Core.dll is added by default as well as a using to Crosser.EdgeNode.Flows. Other usings added by default are:
- System
- System.Collections.Generic
| Name | Requirements | Purpose | Default |
|---|---|---|---|
| Init | string | The C# code to execute at startup | In here you can set variables in the `flowContext`. Note that you can't access local variables declared in Init in the Main/Source code |
| Main | string | The C# code to execute every time the module get data | |
| Target Property | string | The property that will hold the result returned by the the code |
Code Context
Inside the Main code you can access a few arguments/methods that was passed in
| Name | Required | Type | Description |
|---|---|---|---|
| msg | - | FlowMessage | The message from the previous module |
| flowContext | - | [Repository] | See examples in the Context Example section |
| Debug | - | Method | Any object passed to Debug will be sent to the UI in a remote session if debug is enabled on the module |
Input
The input has no specific requirements. Whatever is being sent to the module will be accessible through the msg variable inside the code module.
Output
The object returned from the code will be passed on the the next module(s). If the result is null the message will not be sent to the next module(s).
Example
You can change the incoming object or create a new message to return. Regardless what you choose to do it is the object returned that is passed on to the next module(s).
// just return a new object
return new {num = 123, str = "ABC"};
// return the data part from the object coming in
// (requires the data property to exists of course)
return msg.data;
Debug Example
You can use the Debug method to send data to the UI when running in a remote session. You can send any object to the Debug method
Debug("Hello World");
Context Example
The contexts are dictionaries of type <string,dynamic> so you can add any object into the contexts. The flowContext allows you to share data between messages received by the module, or with other modules in the same flow.
// Will set the key "foo" to the value "10"
flowContext.AddOrUpdate("foo",10);
// Get the value for the key "foo"
var v = flowContext.GetById("foo");
Adding references
You can add references by using the standard #r "<FullNamespaceHere>".
Example using HttpClient in System.Net.Http
#r "System.Net.Http"
var client = new System.Net.Http.HttpClient();
var response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
var str = await response.Content.ReadAsStringAsync ();
return new {data = str};
Adding scripts
You can add .csx files by using the standard #load "<filename.csx>".
Scripts can be added as resources.
// myscript.csx
using System;
public void Hello(string w) {
Console.WriteLine($"Hello {w}");
}
#load "./data/flowresources/myscript.csx"
Hello("World");
Search Documentation
Process & Analyze
Custom Code & ML