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

@VenkatM @Srini84 @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.

Download the package:
dotNETRunner.Activities.1.3.1.nupkg (11.8 KB)

Download the documentation:
dotNETRunner.1.3.1.pdf (305.4 KB)

Install the package:
image

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

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)

Addendum 11.09.2022: The approach presented here is deprecated, because it can’t work in the Windows compatibility mode.

10 Likes

@StefanSchnell Thanks for your great sharing.

I have two questions.
I wonna know what “Input Assembly” should I use? Will it be the same as “using statement” ?
Should I create .dll initiatively ? or I can just key the string in the textbox.

the second question is: “Input Code” is the variable from Read Text File’output ?

Thanks!

1 Like

Hello @lojyehuang,
thanks for your reply.
To your questions:

  1. No, with the using directives you define the namespaces you want to use. In the .NET API browser you can find the necessary assemblies. They can have the same name, but it doesn’t have to.
    image
    The extension .dll is necessary.

  2. Yes.

Hint: I have updated the activity above to version 1.2.0, with much more possibilities.

Let us know your results.
Best regards
Stefan

1 Like

@StefanSchnell
Thanks a lot. That solve my question.
image

1 Like

@StefanSchnell Thanks for sharing. This makes UiPath much more programmer friendly.

I am trying to use this and my code compiles, but I am not able to initialise an instance with parameters. I got the exception Constructor on type not found. Is it possible to initialise with parameters or I put constants in the class instead? If it is possible, are there any examples?

Thanks

1 Like

@acyli

Hello Alex,
interesting requirement, no, unfortunately it is not possible - at the moment.
Best regards
Stefan

@acyli

Hello Alex,
new release is available, which offers the possibility to use constructors.
Let us know your results.
Best regards
Stefan

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

namespace Foo {

  public class Bar {

    public int pub_i;
	public string pub_text;

    public Bar(string i, string text) {
      pub_i = int.Parse(i);
	  pub_text = text;
    }

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

    public int Say42Func() {
      return 42 + pub_i;
    }

  }

}

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

image

image

image

2 Likes