/* Made by Lonami Exo
* (C) LonamiWebs
* 13 February 2015 */
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
public static class Invoker
{
#region Private variables
static readonly Dictionary<string, Assembly> LoadedAssemblies = new Dictionary<string, Assembly>();
#endregion
#region Public methods
/// <summary>
/// Invokes a method from a given dll
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="method">The method</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
/// <param name="args">The arguments in which the method will be called</param>
/// <returns>The return of the method</returns>
public static object[] InvokeMethod(string dllPath, string method, string className = "", params object[] args)
{
var types = GetTypes(dllPath, className, method);
var results = new object[types.Length];
for (int i = 0; i < types.Length; i++)
results[i] = types[i].InvokeMember(method, BindingFlags.InvokeMethod, null,
Activator.CreateInstance(types[0]), args);
return results;
}
/// <summary>
/// Gets the value of the specified property
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="property">The property name</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
/// <returns>The value of the property, null if it doesn't exist</returns>
public static object GetPropertyValue(string dllPath, string property, string className) {
var types = GetTypes(dllPath, className);
if (types.Length > 0 && ContainsProperty(types[0], property))
return types[0].GetProperty(property).GetValue(types[0]); // TODO Not sure if GetPropertyValue works!
return null;
}
/// <summary>
/// Sets the value to the specified property
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="property">The property name</param>
/// <param name="value">The new value</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
public static void SetPropertyValue(string dllPath, string property, object value, string className) {
var types = GetTypes(dllPath, className);
if (types.Length > 0 && ContainsProperty(types[0], property))
types[0].GetProperty(property).SetValue(types[0], value); // TODO Not sure if SetPropertyValue works!
}
/// <summary>
/// Checks whether a dll has or not the specified method. Note that this IS case sensitive
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="method">The method to check</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
/// <returns>True if the method was found, otherwise false</returns>
public static bool HasMethod(string dllPath, string method, string className = "")
{
var types = GetTypes(dllPath, className);
if (types.Length <= 0)
return false;
bool has = false;
foreach (var type in types) {
if (type.GetMethods().Any(m => m.Name == method)) {
has = true;
break;
}
}
return has;
}
/// <summary>
/// Enumerates the methods contained in the specified dll
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
/// <returns></returns>
public static MethodInfo[] EnumerateMethods(string dllPath, string className = "") {
var types = GetTypes(dllPath, className);
return types.Length < 0 ? new MethodInfo[0] : types[0].GetMethods();
}
/// <summary>
/// Enumerate the properties contained in the specified dll
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
/// <returns></returns>
public static PropertyInfo[] EnumerateProperties(string dllPath, string className = "") {
var types = GetTypes(dllPath, className);
return types.Length < 0 ? new PropertyInfo[0] : types[0].GetProperties();
}
/// <summary>
/// Enumerate the members contained in the specified dll
/// </summary>
/// <param name="dllPath">The dll</param>
/// <param name="className">If you don't know the class name, you can omit this paramether</param>
/// <returns></returns>
public static MemberInfo[] EnumerateMembers(string dllPath, string className = "") {
var types = GetTypes(dllPath, className);
return types.Length < 0 ? new MemberInfo[0] : types[0].GetMembers();
}
#endregion
#region Private methods
static Assembly AssemblyByName(string name) {
if (!LoadedAssemblies.ContainsKey(name))
LoadedAssemblies.Add(name, Assembly.LoadFile(name));
return LoadedAssemblies[name];
}
static Type[] GetTypes(string dllPath, string className) {
var dll = AssemblyByName(dllPath);
return className.Length == 0 ? dll.GetExportedTypes()
: dll.GetExportedTypes().Where(t => t.Name == className).ToArray();
}
static Type[] GetTypes(string dllPath, string className, string method) {
var dll = AssemblyByName(dllPath);
return className.Length == 0 ?
dll.GetExportedTypes().Where(t => t.GetMethods().Any(m => m.Name == method)).ToArray()
: dll.GetExportedTypes().Where(t => t.Namespace == className && t.GetMethods()
.Any(m => m.Name == method)).ToArray();
}
static bool ContainsProperty(Type t, string property) {
return t.GetProperties().Any(p => p.Name == property);
}
#endregion
/* In NET 4.0 you can do the next:
*
dynamic c = Activator.CreateInstance(type);
c.Method(args);
*/
}
This class allows you to easily invoke methods from other assemblies, such as dynamic libraries or executables
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.