Tip: Use C#, VB.NET or JScript code as UiPath Studio Activity

@VenkatM @ksrinu070184 @AndrewHall

Venkat asked here about the possibility to use C# code inside UiPath. The answer from Srinivas links to a post of Andrew about the temporarily removing of C# support. Current status: It is not possible to use C# code with UiPath Studio.

I developed a half year ago for SAP IRPA a library which offers the possibility to execute C# or VB.NET code seamlessly in IRPA, it called dotNETRunner. With Venkats email it comes into my attention that the C# support is temporarily disabled in UiPath Studio. An implementation for UiPath on this basis therefore makes sense now.

dotNETRunner includes only one property and three methods.

Add Assembly property to add references to other dotNET assemblies.

Clear Assemblies method to delete all referenced assemblies, if necessary.

CompCode method to compile the code, with the input parameters

  1. Language as string, allowed are CS for C#, VB for VB.NET or JS for JScript.
  2. Code as string.
  3. IncludeDebugInformation as boolean, optional, default value true, generate debug information.
  4. WarningLevel as int, optional, default value 3, level at which the compiler starts displaying warnings.

Run method to execute the code. The run method has four input parameters:

  1. Instance of the class as string.
  2. Method to call as string.
  3. Parameters as string, optional, default value empty string. Parameters are always strings, so it is necessary to convert it in the code in the right format.
  4. Separator for the parameters as string, optional, default value comma.

Download the package:
dotnetrunner.activities.1.0.3.zip (7.1 KB)

Install the package:
image

After the installation you find an activity called dotNetRunner with the property and the two methods:
image

You can use it very easily:
image

Read the C# source via Read Text File activity. Here an example of Test.dotNETRunner.cs:

//-Begin----------------------------------------------------------------

using System.Windows.Forms;

namespace Foo {
  public class Bar {

    public string SayHelloFunc() {
      return "Hello World";
    }

    public int Say42Func() {
      return 42;
    }

    public double Say166Func() {
      return 166.0;
    }

    public int Add(string v1, string v2) {
      int val1 = System.Convert.ToInt32(v1);
      int val2 = System.Convert.ToInt32(v2);
      int res = val1 + val2;
      return res;
    }

    public void Yell() {
      MessageBox.Show("Hello World with native dotNET", "C#");
    }

  }
}

//-End------------------------------------------------------------------

Add the assembly:
image

Compile the code:
image

Execute the method SayHelloFunc:
image

And you will get this result:
image

Or you can call any other method, e.g. Yell:
image

With dotNETRunner you can execute C#, VB.NET or JScript methods.

'-Begin-----------------------------------------------------------------

Imports System.Windows.Forms

Namespace Foo
  Public Class Bar

    Public Function SayHelloFunc() As String
      SayHelloFunc = "Hello World"
    End Function

    Public Function Say42Func() As Integer
      Say42Func = 42
    End Function

    Public Function Say166Func() As Double
      Say166Func = 166.0
    End Function

    Public Function Add(val1 As String, val2 As String) As Integer
      Add = CInt(val1) + CInt(val2)
    End Function

    Public Function Yepp(val1 As String, val2 As String) As String
      Yepp = val1 & val2
    End Function

    Public Sub Yell()
      MessageBox.Show("Hello World with native dotNET", "VB.NET")
    End Sub

  End Class
End Namespace

'-End-------------------------------------------------------------------

image

Here a JScript example:

//-Begin----------------------------------------------------------------

import System.Windows.Forms;

package Foo {

  class Bar {

    function SayHelloFunc() {
      return "Hello World";
    }

    function Say42Func() {
      return 42;
    }

    function Say166Func() {
      return 166.0;
    }

    function Add(v1, v2) {
      var res = parseInt(v1) + parseInt(v2);
      return res;
    }

    function Yell() {
      MessageBox.Show("Hello World with native dotNET", "JScript");
    }

  }

}

//-End------------------------------------------------------------------

Hope this is a valid alternative until UiPath supports C# again.

My favorite example is the display of Mandelbrot.

Here the code: Mandelbrot.zip (1.5 KB)

3 Likes

@Lahiru.Fernando

I have to mention you in a reply, because only three users can mentioned in a post.

1 Like