06-01-21 04:28 PM
//-Begin----------------------------------------------------------------
using System;
namespace test {
public class nuget {
public string mtest(string Input = "") {
if(String.IsNullOrEmpty(Input)) {
return "Hello World";
} else {
return "Hello " + Input;
}
}
}
}
//-End------------------------------------------------------------------
In the next step I installed this NuGet package at the target system in the UserProfile directory.
nuget.exe Install TestNuget -Source "%cd%" -OutputDirectory "%USERPROFILE%\.nuget"
And with a Code Stage I call the method of this library dynamically, independently from the customizing in the Code Options.
//-Begin----------------------------------------------------------------
Result = string.Empty;
ErrRet = string.Empty;
string UserDir = Environment.GetEnvironmentVariable("UserProfile");
string DLLPath = UserDir + "\\.nuget\\TestNuget.1.0.0\\lib\\net40";
try {
//-Loads assembly-----------------------------------------------------
Assembly TestNugetDLL = Assembly.LoadFile(DLLPath + "\\TestNuget.dll");
//-Gets the class type------------------------------------------------
Type TestNugetClass = TestNugetDLL.GetType("test.nuget");
if(TestNugetClass == null) {
return;
}
//-Gets the method----------------------------------------------------
MethodInfo mtest = TestNugetClass.GetMethod("mtest");
//-Creates instance of the class--------------------------------------
object TestNugetObj = Activator.CreateInstance(TestNugetClass);
//-Call method without parameter--------------------------------------
var oResult = mtest.Invoke(TestNugetObj, new object[] { null });
Result = oResult.ToString();
Result += Environment.NewLine;
//-Call method with parameter-----------------------------------------
oResult = mtest.Invoke(TestNugetObj, new object[] { "Stefan" });
Result += oResult.ToString();
} catch(Exception ex) {
ErrRet = ex.Message;
}
//-End------------------------------------------------------------------
Only the namespace System.Reflection must be included.
Yes, including the library and calling the methods is "a bit cumbersome". But this approach offers a great possibility, to call methods of an assembly which are not in the program directory of BP. This separation simplifies especially the deployment of libraries.
Enjoy it.
Best regards
Stefan
06-01-21 08:41 PM
07-01-21 06:14 AM
//-Begin----------------------------------------------------------------
//-Initialization of output variables-----------------------------------
Result = string.Empty;
ErrorMessage = string.Empty;
//-Check if file exists-------------------------------------------------
if(!File.Exists(AutoItFileName)) {
ErrorMessage = "File not found";
Console.WriteLine(ErrorMessage);
return;
}
string UserDir = Environment.GetEnvironmentVariable("UserProfile");
string DLLPath = UserDir + "\\.nuget\\AutoIt4dotNET.1.0.0\\lib\\net40";
try {
//-Loads AutoIt assembly----------------------------------------------
Assembly AutoItDLL = Assembly.LoadFile(DLLPath + "\\AutoIt4dotNET.dll");
//-Gets the class type------------------------------------------------
Type AutoItClass = AutoItDLL.GetType("AutoIt4dotNET");
if(AutoItClass == null) {
return;
}
//-Gets the method----------------------------------------------------
MethodInfo Run = AutoItClass.GetMethod("Run");
//-Creates instance of the class--------------------------------------
object AutoIt = Activator.CreateInstance(AutoItClass);
//-Call method--------------------------------------------------------
var oResult = Run.Invoke(AutoIt, new object[] {
AutoItFileName,
Arguments,
Convert.ToInt32(TimeOut),
x64,
"", //-Path to AutoIt if local available, not necessary here
OutputDebug
});
Result = oResult.ToString();
} catch(Exception ex) {
ErrorMessage = ex.Message;
}
//-End------------------------------------------------------------------
07-01-21 04:59 PM
08-01-21 05:51 AM
09-01-21 05:23 PM
//-Begin----------------------------------------------------------------
//-Initialization of output variables-----------------------------------
Result = string.Empty;
ErrorMessage = string.Empty;
//-Check if file exists-------------------------------------------------
if(!File.Exists(AutoItFileName)) {
ErrorMessage = "File not found";
Console.WriteLine(ErrorMessage);
return;
}
string UserDir = Environment.GetEnvironmentVariable("UserProfile");
string DLLPath = UserDir + "\\.nuget\\AutoIt4dotNET.1.0.0\\lib\\net40";
try {
//-Loads AutoIt assembly----------------------------------------------
Assembly AutoItDLL = Assembly.LoadFile(DLLPath + "\\AutoIt4dotNET.dll");
//-Gets the class type------------------------------------------------
Type AutoItClass = AutoItDLL.GetType("AutoIt4dotNET");
if(AutoItClass == null) {
return;
}
//-Gets the method----------------------------------------------------
MethodInfo Run = AutoItClass.GetMethod("Run");
//-Creates instance of the class--------------------------------------
dynamic AutoIt = Activator.CreateInstance(AutoItClass);
//-Call method--------------------------------------------------------
Result = AutoIt.Run(
AutoItFileName,
Arguments,
Convert.ToInt32(TimeOut),
x64,
"", //-Path to AutoIt if local available, not necessary here
OutputDebug
);
} catch(Exception ex) {
ErrorMessage = ex.Message;
}
//-End------------------------------------------------------------------
11-01-21 11:39 AM
11-01-21 07:07 PM