示例#1
0
        private static List <RemoteServiceEntity> GetRemoteServiceList()
        {
            var typeList          = new List <Type>(Assembly.Load("Interface").GetTypes());
            var remoteServiceList = new List <RemoteServiceEntity>();

            foreach (var type in typeList)
            {
                var resModuleName = type.Name.TrimStart('I');
                var methods       = type.GetMethods();
                foreach (var methodInfo in methods)
                {
                    var attr = methodInfo.GetAttribute <RemarkAttribute>();
                    if (attr != null)
                    {
                        var remoteService = new RemoteServiceEntity();
                        var parms         = methodInfo.GetParameters();
                        var parmsStr      = new StringBuilder("(");
                        foreach (var parameterInfo in parms)
                        {
                            var parmName = parameterInfo.Name;
                            var parmType = parameterInfo.ParameterType;
                            parmsStr.AppendFormat("{0} {1},", parmType, parmName);
                        }
                        var returnClass = methodInfo.ReturnType;
                        remoteService.InterfaceNo     = attr.No;
                        remoteService.ResModule       = resModuleName;
                        remoteService.MethodBehavior  = methodInfo.Name;
                        remoteService.InterfaceRemark = attr.Notes;
                        remoteService.ParmsRemark     = parms.Length == 0 ? "" : attr.ParmsNote + " :" + parmsStr.ToString().TrimEnd(',') + ")";
                        remoteService.ReturnClass     = returnClass.IsGenericType ? returnClass.GetGenericArguments()[0].Name : returnClass.ToString();
                        remoteService.ReturnRemark    = attr.ReturnNote;
                        remoteServiceList.Add(remoteService);
                    }
                }
            }
            //remoteServiceList.Sort();
            //remoteServiceList.Sort(delegate(RemoteServiceEntity x, RemoteServiceEntity y)
            //{
            //    if (x.InterfaceNo == null && y.InterfaceNo == null) return 0;
            //    if (x.InterfaceNo == null) return -1;
            //    if (y.InterfaceNo == null) return 1;
            //    return String.CompareOrdinal(x.InterfaceNo, y.InterfaceNo);
            //});
            remoteServiceList.Sort(delegate(RemoteServiceEntity x, RemoteServiceEntity y)
            {
                return(String.CompareOrdinal(x.ResModule, y.ResModule));
            });

            return(remoteServiceList);
        }
示例#2
0
        /// <summary>
        /// 生成前端API文件
        /// </summary>
        public static void WriteWebAPI()
        {
            var typeList          = new List <Type>(Assembly.Load("Interface").GetTypes());
            var remoteServiceList = new List <RemoteServiceEntity>();

            foreach (var type in typeList)
            {
                var resModuleName = type.Name.TrimStart('I');
                var methods       = type.GetMethods();
                foreach (var methodInfo in methods)
                {
                    var attr = methodInfo.GetAttribute <RemarkAttribute>();
                    if (attr != null)
                    {
                        var remoteService = new RemoteServiceEntity();
                        var parms         = methodInfo.GetParameters();
                        var parmsStr      = new StringBuilder("(");
                        foreach (var parameterInfo in parms)
                        {
                            var parmName = parameterInfo.Name;
                            var parmType = parameterInfo.ParameterType;
                            parmsStr.AppendFormat("{0} {1},", parmType, parmName);
                        }
                        remoteService.InterfaceNo     = attr.No;
                        remoteService.ResModule       = resModuleName;
                        remoteService.MethodBehavior  = methodInfo.Name;
                        remoteService.InterfaceRemark = attr.Notes;
                        remoteService.ParmsRemark     = parms.Length == 0 ? "" : attr.ParmsNote + " :" + parmsStr.ToString().TrimEnd(',') + ")";
                        remoteService.ReturnRemark    = attr.ReturnNote;
                        remoteServiceList.Add(remoteService);
                    }
                }
            }
            remoteServiceList.Sort((x, y) => String.CompareOrdinal(x.ResModule, y.ResModule));



            FileStream   fs = new FileStream("E:\\APIs.js", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            //开始写入
            sw.WriteLine("/* ****************");
            sw.WriteLine("*	");
            sw.WriteLine("*	平台数据API列表");
            sw.WriteLine("*	");
            sw.WriteLine("*	@后台自动生成");
            sw.WriteLine("*	" + DateTime.Now.ToShortDateString());
            sw.WriteLine("* ");
            sw.WriteLine("**************** */");
            sw.WriteLine("import apiService from './apiService'");
            sw.WriteLine("const api = {");

            var sOldModule = "";
            var moduleList = new List <string>();
            var nIndex     = 0;

            foreach (var item in remoteServiceList)
            {
                nIndex++;
                if (moduleList.Contains(item.MethodBehavior))
                {
                    continue;
                }
                moduleList.Add(item.MethodBehavior);
                if (sOldModule != item.ResModule)
                {
                    if (sOldModule != "")
                    {
                        sw.WriteLine("  }, ");
                    }

                    sw.WriteLine("  " + item.ResModule + (item.ResModule == "Bed" ? "s" : "") + ": {");
                }

                sw.WriteLine("      " + item.MethodBehavior + ": function (data) {");
                sw.WriteLine("          return apiService('" + item.ResModule + "', '" + item.MethodBehavior + "', '" + GetInvokeType(item.MethodBehavior) + "', data)");
                sw.Write("      }");

                if (nIndex < remoteServiceList.Count)
                {
                    if (remoteServiceList[nIndex].ResModule == item.ResModule)
                    {
                        sw.Write(",");
                    }
                }
                sw.WriteLine();
                sOldModule = item.ResModule;
            }
            sw.WriteLine("	}");
            sw.WriteLine("}");
            sw.WriteLine(" ");
            sw.WriteLine("export default api");
            sw.WriteLine("	");

            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }