You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.0 KiB
102 lines
4.0 KiB
4 years ago
|
using System;
|
||
|
using System.Reflection;
|
||
|
|
||
|
namespace Common.AssemblyUtil
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 功能:程序集工厂
|
||
|
/// 作者:王昊昇
|
||
|
/// 时间:2012.2.8
|
||
|
/// </summary>
|
||
|
public static class AssemblyFactory
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 创建类实例
|
||
|
/// </summary>
|
||
|
/// <param name="packagePath">类所在包的系统文件路径</param>
|
||
|
/// <param name="className">类的完整路径</param>
|
||
|
/// <returns>返回已经实例化的类实例</returns>
|
||
|
public static object CreateInstance(string packagePath, string className)
|
||
|
{
|
||
|
//加载程序集
|
||
|
Assembly ass = Assembly.Load(packagePath);
|
||
|
|
||
|
//获取类型
|
||
|
Type type = ass.GetType(className);
|
||
|
|
||
|
//创建类实例
|
||
|
return Activator.CreateInstance(type);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建类实例(动态链接库文件不需要与调用者在同一文件夹下)
|
||
|
/// </summary>
|
||
|
/// <param name="ddlPath">动态链接库文件路径</param>
|
||
|
/// <param name="className">类的完整路径</param>
|
||
|
/// <returns>返回已经实例化的类实例</returns>
|
||
|
public static object CreateInstanceAnywhere(string ddlPath, string className)
|
||
|
{
|
||
|
//加载程序集
|
||
|
Assembly ass = Assembly.LoadFile(ddlPath);
|
||
|
|
||
|
//获取类型
|
||
|
Type type = ass.GetType(className);
|
||
|
|
||
|
//创建类实例
|
||
|
return Activator.CreateInstance(type);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取动态链接库文件中所有模块
|
||
|
/// </summary>
|
||
|
/// <param name="filePath">动态链接库文件完整路径</param>
|
||
|
/// <returns>返回模块数组</returns>
|
||
|
public static Module[] GetModules(string filePath)
|
||
|
{
|
||
|
//获取程序集
|
||
|
Assembly ass = Assembly.LoadFile(filePath);
|
||
|
|
||
|
//获取所有模块信息
|
||
|
return ass.GetModules();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 调用函数
|
||
|
/// </summary>
|
||
|
/// <param name="instance">函数所在类的实例</param>
|
||
|
/// <param name="functionName">函数名称</param>
|
||
|
/// <param name="parameter">调用的方法或构造函数的参数列表。这是一个对象数组,这些对象与要调用的方法或构造函数的参数具有相同的数量、顺序和类型。如果没有任何参数,则 parameters应为 null。如果此实例所表示的方法或构造函数采用 ref 参数(在 Visual Basic 中为 ByRef),使用此函数调用该方法或构造函数时,该参数不需要任何特殊特性。如果数组中的对象未用值来显式初始化,则该对象将包含该对象类型的默认值。对于引用类型的元素,该值为null。对于值类型的元素,该值为 0、0.0 或 false,具体取决于特定的元素类型。</param>
|
||
|
/// <returns></returns>
|
||
|
public static object InvokeMethod(object instance, string functionName, object[] parameter)
|
||
|
{
|
||
|
//获取函数定义
|
||
|
MethodInfo mi = instance.GetType().GetMethod(functionName);
|
||
|
|
||
|
//执行函数
|
||
|
return mi.Invoke(instance, parameter);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 绑定枚举对象值
|
||
|
/// </summary>
|
||
|
/// <param name="t">枚举对象类型</param>
|
||
|
/// <param name="value">包含要转换的值或名称的字符串。</param>
|
||
|
/// <returns></returns>
|
||
|
public static object BindEnumerator(Type t, string value)
|
||
|
{
|
||
|
return Enum.Parse(t, value, true);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 绑定枚举对象值
|
||
|
/// </summary>
|
||
|
/// <typeparam name="T">枚举类型</typeparam>
|
||
|
/// <param name="value">包含要转换的值或名称的字符串。</param>
|
||
|
/// <returns>枚举</returns>
|
||
|
public static T BindEnumerator<T>(string value) where T : new()
|
||
|
{
|
||
|
return (T)Enum.Parse(typeof(T), value, true);
|
||
|
}
|
||
|
}
|
||
|
}
|