using System; using System.Globalization; using System.IO; using System.Reflection; using System.Windows.Forms; namespace Xylem.Common.Utils.AssemblyLoader { /// /// Loader for assemblies (DLLs). /// [Serializable] public class AssemblyLoader : MarshalByRefObject { /// /// Reminder of loaded assembly. /// private Assembly _loadedAssembly; /// /// Reminder of loaded class object. /// private Object _classObject; /// /// Invoke a member of an already loaded assembly . /// This will be used to call a member function of the class and dispatch data. /// /// class needed for reference /// /// /// true if member function invoked /// /// - Initial /// 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; } } /// /// Load assembly and return object pointer to class name. /// /// path to collection of assemblies /// assembly containing the required function /// class needed for reference /// objectPointer to class name /// /// - Initial /// 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; } } /// /// 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. /// /// path to collection of assemblies /// assembly containing the required function /// true if assemblies are loaded /// /// - Initial /// /// /// - Removed load to ram as it does not work as expected. /// 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; } } /// /// Getting the reference of function in already loaded assembly. /// /// class needed for reference /// function needed for reference /// parameters /// Object to function in assembly /// /// - Initial /// 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; } } ///// ///// Getting the reference of function in already loaded assembly. ///// ///// class needed for reference ///// function needed for reference ///// parameters ///// Object to function in assembly ///// ///// - Initial ///// //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; // } //} } }