public static string ModelAnalysis(Type type, string fileName, CategoryEnum category, ref string name, int i = 0) { if (type.IsConstructedGenericType) { name = type.GetGenericArguments()[0].Name; } else { name = type.Name; } StringBuilder sb = new StringBuilder(); sb.Append("message "); if (!types.ContainsKey(type.FullName)) { bool isRequest = false; if (CategoryEnum.Request == category) { isRequest = true; name += "Request"; sb.AppendLine(name + "{"); sb.Append("\tstring request_id=" + ++i).AppendLine(";"); } else if (CategoryEnum.Response == category) { var oldName = name; name += "Response"; sb.AppendLine(name + "{"); sb.Append("\tint32 code =" + ++i).AppendLine(";"); sb.Append("\tstring message =" + ++i).AppendLine(";"); sb.Append("\t" + oldName + " data=" + ++i).AppendLine(";"); //如果是Class 需要获取父类的属性 if (type.BaseType != null) { if (!entityTypes.ContainsKey(type.Name)) { string newName = ""; var entity = TypeConvert.ModelAnalysis(type, fileName, CategoryEnum.Entity, ref newName); entityTypes.Add(newName, entity); sb.AppendLine("}"); return(sb.ToString()); } } } else if (CategoryEnum.Entity == category) { sb.AppendLine(name + " {"); } for (i = 0; i < type.GetProperties().Length; i++) { var property = type.GetProperties()[i]; sb.AppendLine("\t" + property.Convert(fileName) + " " + ParamtersFormat(property.Name) + "=" + (isRequest ? i + 2 : i + 1) + ";"); } sb.AppendLine("}"); types.Add(type.FullName, new Entity() { IsCommon = false, Value = sb.ToString(), FileName = fileName }); return(sb.ToString()); } else { types[type.FullName].IsCommon = true; if (types[type.FullName].FileName != fileName) { return(types[type.FullName].Value); } else { return(null); } } }
static void Main(string[] args) { /** * exe */ string arg = ""; var app = new CommandLineApplication(); app.Command("path", (command) => { command.Description = "配置解析路径"; command.HelpOption("-?|-h|--help"); var result = command.Argument("[path]", "", false); command.OnExecute(() => { arg = result.Value; Console.WriteLine(result.Value); return(1); }); }); app.HelpOption("-? | -h | --help"); app.Execute(args); if (!string.IsNullOrEmpty(arg)) { var exePath = Path.GetDirectoryName(Assembly.LoadFrom(arg).CodeBase); Regex appPathMatcher = new Regex(@"(?<!fil)[A-Za-z]:\\+[\S\s]*?(?=\\+bin)"); var appRoot = appPathMatcher.Match(exePath).Value + @"\protos"; if (!Directory.Exists(appRoot)) { Directory.CreateDirectory(appRoot); string fileName = string.Empty; var interfaces = Assembly.LoadFrom(arg).DefinedTypes; foreach (var item in interfaces) { if (item.IsInterface) { List <string> methods = new List <string>(); LinkedList <string> requestModels = new LinkedList <string>(); fileName = appRoot + @"\" + item.Name + ".proto"; FileStream fs = new FileStream(fileName, FileMode.CreateNew); StreamWriter sw = new StreamWriter(fs); List <Entity> header = new List <Entity>() { new Entity() { Value = "syntax = \"proto3\";" }, new Entity() { Value = "package " + item.Name + "Service;" } }; header.ForEach(a => sw.WriteLine(a.Value)); foreach (var method in item.GetMethods()) { var requestModelName = string.Empty; var responseModelName = string.Empty; Type type = null; if (method.GetParameters()[0].ParameterType.IsGenericType) { type = method.GetParameters()[0].ParameterType; var result = TypeConvert.ModelAnalysis(type, fileName, CategoryEnum.Request, ref requestModelName); if (result != null) { requestModels.AddFirst(result); } } else { type = method.GetParameters()[0].ParameterType; var result = TypeConvert.ModelAnalysis(type, fileName, CategoryEnum.Request, ref requestModelName); if (result != null) { requestModels.AddLast(result); } } if (method.ReturnType.IsGenericType) { type = method.ReturnType.GetGenericArguments().FirstOrDefault(); var result = TypeConvert.ModelAnalysis(type, fileName, CategoryEnum.Response, ref responseModelName); if (result != null) { TypeConvert.responseModels.Add(responseModelName, result); } } StringBuilder sb = new StringBuilder(); sb.Append("\trpc ").Append(method.Name).Append("(") .Append(requestModelName).Append(") returns(") .Append(responseModelName).Append(")").AppendLine("{"); sb.AppendLine("\t}"); methods.Add(sb.ToString()); } sw.WriteLine("service " + item.Name + "Service {"); methods.ForEach(a => sw.WriteLine(a)); sw.WriteLine("}"); sw.WriteLine("/*------------Request-------------*/"); requestModels.ToList().ForEach(a => sw.WriteLine(a)); sw.WriteLine("/*------------Response-------------*/"); TypeConvert.responseModels.ToList().ForEach(a => sw.WriteLine(a.Value)); sw.WriteLine("/*------------Entity---------------*/"); TypeConvert.entityTypes.ToList().ForEach(a => sw.WriteLine(a.Value)); sw.Flush(); sw.Close(); fs.Close(); } } } } Console.ReadKey(); }