I am not a C# developer expert.
I need to load a specific C# DLL under condition to execute a function named Sum
.
Here is the DLL code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testdll
{
public class Class1
{
public int Sum(int a, int b)
{
return a + b;
}
}
}
Can someone confirm that the DLL is correctly unloaded when I am out of the context of the condition?
if(condition)
{
Assembly myassembly = Assembly.LoadFrom (dllPath);
Type type = myassembly.GetType("testdll.Class1");
object instance = Activator.CreateInstance(type);
MethodInfo[] methods = type.GetMethods();
object res = null;
// Display information for all methods.
for (int i = 0; i < methods.Length; i++)
{
MethodInfo myMethodInfo = (MethodInfo)methods[i];
if (myMethodInfo.Name == "Sum")
{
res = methods[i].Invoke(instance, new object[] { 3, 5});
break;
}
}
if(res != null)
MessageBox.Show("OK");
else
MessageBox.Show("KO");
}