コード例 #1
0
        public static void Test()
        {
            var convertInfo = ClassConvert.Convert(System.Reflection.Assembly.GetAssembly(typeof(ClassTest)));

            convertInfo.ForEach(b => b.CreateCode());
            //Console.WriteLine(code);
            Console.ReadLine();
        }
コード例 #2
0
        public string CreateCode()
        {
            //var packageName = ServiceType.Name;
            var lines = new List <string>();

            lines.Add("syntax = \"proto3\";");
            lines.Add($"option csharp_namespace = \"{Namespace}\";");
            lines.Add($"package {PackageName};");

            //var serviceName = ServiceType.Name;
            //methods
            lines.Add("service " + ServiceName + " {");
            foreach (var method in ServiceType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                var arguments = method.GetParameters();
                if (arguments.Length > 1)
                {
                    throw new Exception($"{method.Name} 参数不能大于一个");
                }
                var args = new List <string>();
                foreach (var f in arguments)
                {
                    var parameType = f.ParameterType;
                    if (!parameType.IsClass || parameType == typeof(string) || typeof(System.Collections.IEnumerable).IsAssignableFrom(parameType))
                    {
                        throw new Exception($"{ServiceType.Name}.{method.Name} 参数必须为class");
                    }
                    args.Add(convertType(parameType));
                    ClassConvert.CreateTypeInfo(this, parameType);
                }

                var returnType = method.ReturnType;
                if (!returnType.IsClass || returnType == typeof(string) || typeof(System.Collections.IEnumerable).IsAssignableFrom(returnType))
                {
                    throw new Exception($"{ServiceType.Name}.{method.Name} 返回类型必须为class");
                }
                ClassConvert.CreateTypeInfo(this, returnType);

                //like  rpc SayHello (HelloRequest) returns (HelloReply);
                lines.Add($"    rpc {method.Name}({string.Join(",", args)}) returns ({convertType(returnType)});");
            }
            lines.Add("}");//end service
            //enum
            foreach (var kv in Enums)
            {
                var type = kv.Value;
                lines.Add("message " + type.Name + "DTO {");
                foreach (var e in Enum.GetValues(type))
                {
                    lines.Add($"    {e} = {(int)e};");
                }
                lines.Add("}");
            }
            //types
            foreach (var kv in ClassTypes)
            {
                var typeInfo = kv.Value;
                lines.Add("message " + typeInfo.Type.Name + "DTO {");
                int i = 0;
                foreach (var p in typeInfo.Fields)
                {
                    i++;
                    lines.Add($"    {convertType(p.Type)} {p.FieldName} = {i};");
                }
                lines.Add("}");
            }

            var code = string.Join("\r\n", lines);

            Console.WriteLine(code);
            var path = System.Environment.CurrentDirectory + "\\Protos";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //path += $"\\{Namespace}";
            //if (!Directory.Exists(path))
            //    Directory.CreateDirectory(path);
            var file = path + $"\\{PackageName}.proto";

            System.IO.File.WriteAllText(file, code);
            return(code);
        }