欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

cSharp:反射 Reflection

程序员文章站 2022-03-20 08:56:44
调用: ......

 

 

     /// <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)
        {
               .....
        }
    
    }
}