csharptools
Service icon

CsharpTools

Stable version 1.0.2 (Compatible with OutSystems 11)
Uploaded
 on 12 December 2023
 by 
5.0
 (3 ratings)
csharptools

CsharpTools

Documentation
1.0.2

List of Files And Directories.

The GetFilesAndDirList allow to retrieve a list of files and directories provided a path of a directory in question. The default directory is the root.

The GetFileFromServer it will allow you to receive the binary of the file that you indicate in the path. 


C# Execute dynamic code.

From the CSharpTools library, import a Server Action called CSharpExecute. 

  1. The ImportNamespaces parameter should contain, if necessary, all the assemblies you need to use in your C# code. If you require an assembly that is not installed on the server (whether from third parties or Microsoft itself), you should add the library through the Integration Studio. The "using System;" statement is automatic; you don't need to add it.
  2. In the CSharpCodeExtension parameter, you should include any custom methods or classes that you need to assist in the execution of your C# code.
  3. The CSharpCode parameter is what triggers your code, meaning the execution of the C# code. It should have a return statement at the end, and this return can be any object. In the end, it is serialized into a JSON string.
  4. The function's return is a structure with a bool indicating success (Success property). If there are compilation errors, the CompilationErrors property contains a string with the errors. There is a Result property, representing the result of the execution in case of success. Finally, there is a property with the generated code as a string, including the main class and the main execution method (CodeGenerated property).

 


Note: The CSharpExecuteAsync has the same parameters.

Example 1: 

In ImportNamespaces put empty string "";

In the CSharpCodeExtension add the following: 

" public long fac(long n) {

    if (n <= 1) return 1;

    else return n*fac(n-1);

}";

In the CSharpCode add the following:

" return fac(12); ".

If you execute the result will be from recursive function fac 479001600.


Example 2: 

In ImportNamespaces put empty string "";

In the CSharpCodeExtension add the following: 

" public float fac(float n) {

    if (n <= 1) return 1;

    else return n*fac(n-1);

}";

In the CSharpCode add the following:

" return fac(45); ".

If you execute the result will be from recursive function fac INF.


Example 3: 

In this example, let's assume you want to use a third-party library in .NET Framework 4. The code for this library is as follows:


And this DLL is available for download, in this case, on Google Drive with access for anyone.


In ImportNamespaces add the following:

"

using System;

using System.Net;

using System.Reflection;

"

In the CSharpCodeExtension put an empty string ""; 


In the CSharpCode add the following:

//1JPzl4dKT_US_6wgaDZ3DGL6dLGKp61cr  : This is the Id of the DLL file in the google drive

string downloadLink = "https://drive.usercontent.google.com/download?id=1JPzl4dKT_US_6wgaDZ3DGL6dLGKp61cr";

byte[] assemblyBytes = new WebClient().DownloadData(downloadLink);

Assembly loadedAssembly = Assembly.Load(assemblyBytes);

Type easyMathType = loadedAssembly.GetType("TestClassLibrary.EasyMath");

dynamic easyMathInstance = Activator.CreateInstance(easyMathType);


return easyMathInstance.fact(65); // replace fact with factfloat; factdecimal; factdouble

 ".

Note: In this example we are downloading a DLL from a Google Drive link and loading it as an assembly into memory, enabling the execution or dynamic manipulation of the code contained in the file.


Disclaimer: 

The code is executed on the server side, using Reflection methods provided by Microsoft in the .NET Framework. For this reason, the code created is the entire responsibility of the developer who wrote it. The developer should be responsible and prevent code injection as well as the misuse of the code they have developed. 

This does not replace Integration Studio; it simply allows for the interpretation and execution of code in a text. Keep in mind that interpreted code is generally slower than compiled code.



1.0.0

List of Files And Directories.

The GetFilesAndDirList allow to retrieve a list of files and directories provided a path of a directory in question. The default directory is the root.

The GetFileFromServer it will allow you to receive the binary of the file that you indicate in the path. 


C# Execute dynamic code.

From the CSharpTools library, import a Server Action called CSharpExecute. 

  1. The ImportNamespaces parameter should contain, if necessary, all the assemblies you need to use in your C# code. If you require an assembly that is not installed on the server (whether from third parties or Microsoft itself), you should add the library through the Integration Studio. The "using System;" statement is automatic; you don't need to add it.
  2. In the CSharpCodeExtension parameter, you should include any custom methods or classes that you need to assist in the execution of your C# code.
  3. The CSharpCode parameter is what triggers your code, meaning the execution of the C# code. It should have a return statement at the end, and this return can be any object. In the end, it is serialized into a JSON string.
  4. The function's return is a structure with a bool indicating success (Success property). If there are compilation errors, the CompilationErrors property contains a string with the errors. There is a Result property, representing the result of the execution in case of success. Finally, there is a property with the generated code as a string, including the main class and the main execution method (CodeGenerated property).

 

Disclaimer: 

The code is executed on the server side, using Reflection methods provided by Microsoft in the .NET Framework. For this reason, the code created is the entire responsibility of the developer who wrote it. The developer should be responsible and prevent code injection as well as the misuse of the code they have developed. 

This does not replace Integration Studio; it simply allows for the interpretation and execution of code in a text. Keep in mind that interpreted code is generally slower than compiled code.