Example #1
0
        public StringBuilder GetSvr()
        {
            Type   type   = this.GetType();
            Uri    url    = HttpContext.Current.Request.Url;
            string script = string.Empty;

            if (!string.IsNullOrEmpty(ReflectionCache.GetInstance().JsFile))
            {
                script = ReflectionCache.GetInstance().JsFile;
            }
            else
            {
                ReflectionCache.GetInstance().JsFile = GetScriptTemplete();
                script = ReflectionCache.GetInstance().JsFile;
            }
            script = script.Replace("%H_DESC%", "通过jQuery.ajax完成服务端函数调用");
            script = script.Replace("%H_DATE%", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            script = script.Replace("%URL%", url.Query.Length > 0 ? url.ToString().Replace(url.Query, "") : url.ToString());
            script = script.Replace("%CLS%", type.Name);

            StringBuilder scriptBuilder = new StringBuilder(script);

            MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (MethodInfo m in methods)
            {
                ResponseAnnotationAttribute resAnn = this.GetAnnation(m.Name);

                scriptBuilder.AppendLine("/*");
                scriptBuilder.AppendLine("功能说明:" + resAnn.MethodDescription);
                scriptBuilder.AppendLine("附加说明:缓存时间 " + resAnn.CacheDurtion.ToString() + " 秒");
                scriptBuilder.AppendLine("输出类型 " + resAnn.ResponseFormat.ToString());
                scriptBuilder.AppendLine("*/");
                if (string.IsNullOrEmpty(resAnn.HttpMethod))
                {
                    resAnn.HttpMethod = "POST";
                }
                string func = GetFunctionTemplete(m, resAnn.ResponseFormat, resAnn.HttpMethod);
                scriptBuilder.AppendLine(func);
            }
            return(scriptBuilder);
        }
Example #2
0
        /// <summary>
        /// 调用本地的方法
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private object Invoke(string methodName, Dictionary <string, object> args)
        {
            MethodInfo methInfo;

            if (ReflectionCache.GetInstance().Get(methodName) == null)
            {
                methInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
                ReflectionCache.GetInstance().Add(methodName, methInfo);
            }
            else
            {
                methInfo = ReflectionCache.GetInstance().Get(methodName);
            }
            if (methInfo == null)
            {
                throw new Exception("指定的逻辑方法不存在");
            }
            object[] parameters = this.GetArguments(methodName, args);
            //object result = methInfo.Invoke(this, parameters);
            object result = ReflectionCache.GetMethodInvoker(methInfo).Invoke(this, parameters);

            return(result);
        }