188 lines
7.6 KiB
C#
188 lines
7.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Xylem.Common.Utils.AssemblyLoader
|
|
{
|
|
|
|
/// <summary>
|
|
/// Loader for assemblies (DLLs).
|
|
/// </summary>
|
|
[Serializable]
|
|
public class AssemblyLoader : MarshalByRefObject
|
|
{
|
|
/// <summary>
|
|
/// Reminder of loaded assembly.
|
|
/// </summary>
|
|
private Assembly _loadedAssembly;
|
|
|
|
/// <summary>
|
|
/// Reminder of loaded class object.
|
|
/// </summary>
|
|
private Object _classObject;
|
|
|
|
/// <summary>
|
|
/// Invoke a member of an already loaded assembly <see cref="LoadAssemblyGetClassObject"/>.
|
|
/// This will be used to call a member function of the class and dispatch data.
|
|
/// </summary>
|
|
/// <param name="className">class needed for reference</param>
|
|
/// <param name="functionName"></param>
|
|
/// <param name="parameters"></param>
|
|
/// <returns>true if member function invoked</returns>
|
|
/// <remarks date="2021-Mar-26" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
public Boolean InvokeMember(String className, String functionName, Object[] parameters)
|
|
{
|
|
if (_classObject == null || _loadedAssembly == null) return false;
|
|
try
|
|
{
|
|
var type = _loadedAssembly.GetType(className);
|
|
var methodInfo = type.GetMethod(functionName);
|
|
return !(methodInfo is null) && (Boolean)methodInfo.Invoke(_classObject, BindingFlags.InvokeMethod, null,
|
|
parameters, CultureInfo.CurrentUICulture);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load assembly and return object pointer to class name.
|
|
/// </summary>
|
|
/// <param name="assemblyPath">path to collection of assemblies</param>
|
|
/// <param name="assemblyName">assembly containing the required function</param>
|
|
/// <param name="className">class needed for reference</param>
|
|
/// <returns>objectPointer to class name</returns>
|
|
/// <remarks date="2021-Mar-26" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
public Object LoadAssemblyGetClassObject(String assemblyPath, String assemblyName, String className)
|
|
{
|
|
try
|
|
{
|
|
_loadedAssembly = Assembly.LoadFrom(Path.Combine(assemblyPath, assemblyName));
|
|
// find the method in fully specified class name
|
|
var type = _loadedAssembly.GetType(className);
|
|
_classObject = type == null ? null : Activator.CreateInstance(type);
|
|
|
|
return _classObject;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loading an assembly set. The assemblies can be referenced on the path on a storage
|
|
/// device, then the files will be locked for any write or erase access.
|
|
/// </summary>
|
|
/// <param name="assemblyPath">path to collection of assemblies</param>
|
|
/// <param name="assemblyName">assembly containing the required function</param>
|
|
/// <returns>true if assemblies are loaded</returns>
|
|
/// <remarks date="2020-Nov-29" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
/// <remarks date="2021-Mar-18" author="Thomas Wiedebusch">
|
|
/// - Removed load to ram as it does not work as expected.
|
|
/// </remarks>
|
|
public Boolean LoadAssembly(String assemblyPath, String assemblyName)
|
|
{
|
|
try
|
|
{
|
|
var assemblyPathName = Path.Combine(assemblyPath, assemblyName);
|
|
_loadedAssembly = Assembly.LoadFrom(assemblyPathName);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Getting the reference of function in already loaded assembly.
|
|
/// </summary>
|
|
/// <param name="className">class needed for reference</param>
|
|
/// <param name="functionName">function needed for reference</param>
|
|
/// <param name="parameters">parameters</param>
|
|
/// <returns>Object to function in assembly</returns>
|
|
/// <remarks date="2020-Nov-29" author="Thomas Wiedebusch">
|
|
/// - Initial
|
|
/// </remarks>
|
|
public Object GetAssemblyFnObject(String className, String functionName, Object[] parameters)
|
|
{
|
|
try
|
|
{
|
|
if (_loadedAssembly == null) return null;
|
|
|
|
// find the method in fully specified class name
|
|
var type = _loadedAssembly.GetType(className);
|
|
return type == null ? null : Activator.CreateInstance(type);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
///// <summary>
|
|
///// Getting the reference of function in already loaded assembly.
|
|
///// </summary>
|
|
///// <param name="className">class needed for reference</param>
|
|
///// <param name="functionName">function needed for reference</param>
|
|
///// <param name="parameters">parameters</param>
|
|
///// <returns>Object to function in assembly</returns>
|
|
///// <remarks date="2020-Nov-29" author="Thomas Wiedebusch">
|
|
///// - Initial
|
|
///// </remarks>
|
|
//public Object GetAssemblyFnObject(String className, String functionName, Object[] parameters)
|
|
//{
|
|
// try
|
|
// {
|
|
// if (_loadedAssembly == null) return null;
|
|
|
|
// Type[] objectTypes = null;
|
|
// var count = 0;
|
|
// if (parameters != null)
|
|
// {
|
|
// objectTypes = new Type[parameters.Length];
|
|
// foreach (var objectParameter in parameters)
|
|
// {
|
|
// if (objectParameter != null) objectTypes[count] = objectParameter.GetType();
|
|
// count++;
|
|
// }
|
|
// }
|
|
|
|
// // find the method in fully specified class name
|
|
// var type = _loadedAssembly.GetType(className);
|
|
// //if (objectTypes == null) return type == null ? null : Activator.CreateInstance(type);
|
|
// var ctorInfo = type.GetConstructor(new Type[]{});
|
|
// if (objectTypes == null) return ctorInfo == null ? null : ctorInfo.Invoke(null);
|
|
|
|
// //var method = type.GetMethod(functionName);
|
|
// //var methods = type.GetMethods();
|
|
// //var method = methods[2];
|
|
// var method = type.GetMethod(functionName, objectTypes);
|
|
// // var method = type.GetMethod(functionName, objectTypes);
|
|
// //var tempObject = Activator.CreateInstance(type);
|
|
// return method == null ? null : method.Invoke(functionName, BindingFlags.InvokeMethod |
|
|
// BindingFlags.Public, null, parameters, CultureInfo.CurrentCulture);
|
|
// //return type.InvokeMember(functionName, BindingFlags.DeclaredOnly | BindingFlags.Public |
|
|
// // BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null,
|
|
// // null, parameters);
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// MessageBox.Show(ex.Message);
|
|
// return null;
|
|
// }
|
|
//}
|
|
}
|
|
}
|