How to use dotNET Assemblies

There are many many dotNET assemblies available. To use an assembly inside an Invoke Code Activity you can install it in the Global Assembly Cache (GAC) and import it, or you can use it directly, the way I present here.

Let us start with an own tiny assembly. This assembly contains only one function, with the name test. This function has one parameter and delivers a string.

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

using System;

public class TestLibrary {

  public string Test(string Name = "World") {
    return "Hello " + Name;
  }

}

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

We compile this file to a DLL.

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe -platform:anycpu -out:TestLibrary.dll -target:library TestLibrary.cs

Copy the assembly TestLibrary.dll into your UiPath project directory. To use the function of this assembly in your Invoke Code Activity it is necessary to do three steps:

  1. Load the assembly with the method LoadFrom.
  2. Get an object that represents the class with GetType.
  3. Create an instance of the class with CreateInstance.

After this steps we can use the function of the assembly in our code.

System.Reflection.Assembly TestLibrary = System.Reflection.Assembly.LoadFrom(@"TestLibrary.dll");
System.Type Class = TestLibrary.GetType("TestLibrary");
dynamic Instance = System.Activator.CreateInstance(Class);

string Result = Instance.Test("Stefan");
System.Console.WriteLine(Result);

Result = Instance.Test();
System.Console.WriteLine(Result);

In this example I try it one time with a parameter and one time without a parameter.

image

On this way it is possible to use dotNET assemblies directly in the code sequence of the Invoke Code Activity.

5 Likes