調用: ...
/// <summary> /// 利用反射調用插件方法 /// 塗聚文(Geovin Du) /// 2019-03-27 /// /// </summary> /// <param name="assemblyName">生成類庫文件名稱</param> /// <param name="className">類名</param> /// <param name="methodName">方法名</param> /// <param name="objParas">參數</param> /// <returns>DataTable</returns> public DataTable getMethod(string assemblyName, string className, string methodName, params object[] objParas) { DataTable dt = new DataTable(); try { Assembly assembly = Assembly.Load(assemblyName);//生成類庫文件名稱 放在BIN文件夾下的類庫文件DLL的名稱 object obj = assembly.CreateInstance("DuCms.Web.Plugin." + assemblyName + "." + className); //類庫名稱+類名稱 Type t = obj.GetType(); //查找匹配的方法 foreach (MethodInfo m in t.GetMethods(BindingFlags.Instance | BindingFlags.Public)) { if (m.Name == methodName && m.GetParameters().Length == objParas.Length) { object obj2 = m.Invoke(obj, objParas); dt = obj2 as DataTable; return dt; } } } catch { //插件方法獲取失敗 } return dt; }
調用:
DataTable backList = getMethod("Feedback", "Feedback", "getFeedbackList", 4, "IsLock=0 and SiteId="+Site.Id); foreach(DataRow dr in backList.Rows) { 。。。。 }
Feedback 類
namespace DuCms.Web.Plugin.Feedback { /// <summary> /// /// </summary> public partial class Feedback : Web.UI.Page { /// <summary> /// 留言分頁列表 /// </summary> /// <param name="page_size">頁面大小</param> /// <param name="page_index">當前頁碼</param> /// <param name="strwhere">查詢條件</param> /// <param name="totalcount">總記錄數</param> /// <returns>DateTable</returns> public DataTable getFeedbackList(int pageSize, int pageIndex, string strWhere, out int totalCount) { ..... } } }