注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路奔跑

奔跑着一路向前...

 
 
 

日志

 
 

C# 关于反射类[System.Reflection] 根据类名 动态调用 类方法  

2011-11-07 10:08:22|  分类: Asp.net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
 /// <summary>
    /// Execute the function of the class
    /// </summary>
    /// <param name="ProjectName">the project name of the class </param>
    /// <param name="spaceName">the naming space name of the class</param>
    /// <param name="className">the name of the class</param>
    /// <param name="functionName">the function name which need to execute</param>
    /// <param name="parametersType">the type of the parameters</param>
    /// <param name="parametersValue">the value of the parameters</param>

    /// <param name="page">if you want to execute the function of the page ,set this parameter E.g. : this.Page</param>
    /// <returns>object</returns>
    public static object OperateFuction(string ProjectName, string spaceName, string className, string functionName, ArrayList parametersTypeStr, object[] parametersValue, out string Error,Page page)
    {
        //define the object which return
        object returnObj;
        //Init
        returnObj = null;
        Error = string.Empty;
        System.Reflection.Assembly asm;
        System.Type type;
        //the function in this page which not need the space name and project name
        if (!"".Equals(className))
        {
            type = Type.GetType(className);
        }else{
            type = page.GetType();
        }

        if (type == null)
        {
            string loadFile = string.Empty;
            loadFile = !"".Equals(ProjectName) ? ProjectName + ".dll" : "__code";
            if (!"__code".Equals(loadFile))
            {
                //get the class which in the other project
                string BasePath = System.AppDomain.CurrentDomain.BaseDirectory;
                BasePath = BasePath.Replace(@"\\", @"\");
                string ProjectDllPath =BasePath+ @"Bin\" + ProjectName + ".dll";
                try
                {
                    asm = System.Reflection.Assembly.LoadFrom(ProjectDllPath);
                }
                catch (System.Exception e)
                {
                 ProjectDllPath = BasePath +  ProjectName + ".dll";
                    asm = System.Reflection.Assembly.LoadFrom(ProjectDllPath);
                }
            }
            else
            {
                //get the class which in the App_Code file
                asm = System.Reflection.Assembly.Load(loadFile);
            }
            className = !"".Equals(spaceName) ? spaceName + "." + className : className;
            //get the  class which needs
            type = asm.GetType(className);
        }
        if (type != null)
        {
            //get the object of the class
            Object obj = Activator.CreateInstance(type);
            // define the parameters of the function
            System.Type[] parametersType = new Type[parametersTypeStr.Count];
            int i = 0;
            foreach (string str in parametersTypeStr)
            {
                parametersType[i] = System.Type.GetType(str);
                i++;
            }
            //get the function of the class
            System.Reflection.MethodInfo method = type.GetMethod(functionName, parametersType);
            if (method != null)
            {
                try
                {
                    // Execute the function of the class
                    returnObj = method.Invoke(obj, parametersValue);
                }
                catch (System.Exception e)
                {
                    Error = e.Message;
                }
            }
        }
        return returnObj;
    }

  评论这张
 
阅读(628)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017