Ejemplo n.º 1
0
        /// <summary>
        /// 生成IDL模板
        /// </summary>
        /// <param name="type"></param>
        /// <param name="Namespace">IDL命名空间</param>
        /// <param name="serviceName">IDL服务名</param>
        /// <param name="filePath"></param>
        public Tuple <string, string, string> Create(Type type, string Namespace, string serviceName)
        {
            List <TypeInfo> types = new List <TypeInfo>(); //实体类型集合
            List <FunInfo>  funs  = new List <FunInfo>();  //方法集合

            foreach (MethodInfo m in type.GetMethods())
            {
                FunInfo fun = new FunInfo();
                funs.Add(fun);

                fun.ReturnType = m.ReturnType.ToString();
                fun.FunName    = m.Name;
                fun.Parameters = new List <Tuple <string, string> >();

                string asName = m.ReturnType.Assembly.FullName.Substring(0, m.ReturnType.Assembly.FullName.IndexOf(","));

                AddType(types, m.ReturnType.FullName, asName, m.ReturnType.BaseType == typeof(System.Enum));

                foreach (var p in m.GetParameters())
                {
                    fun.Parameters.Add(Tuple.Create(p.ParameterType.ToString(), p.Name));

                    string asPName = p.ParameterType.Assembly.FullName.Substring(0, p.ParameterType.Assembly.FullName.IndexOf(","));

                    AddType(types, p.ParameterType.FullName, asPName, p.ParameterType.BaseType == typeof(System.Enum));
                }
            }

            types.Sort((x, y) => { return(y.IsEnum.CompareTo(x.IsEnum)); });


            //--------------------------------------------------//

            if (string.IsNullOrEmpty(Namespace))
            {
                Namespace = type.Namespace;
            }

            if (string.IsNullOrEmpty(serviceName))
            {
                serviceName = type.Name + "Thrift";
            }

            System.Text.StringBuilder str = new StringBuilder();

            str.AppendLine($"namespace csharp   {Namespace}");
            str.AppendLine($"namespace java {Namespace}");
            str.AppendLine($"namespace cpp  {Namespace}");

            str.AppendLine();


            foreach (var itemType in types)
            {
                if (isSystemType(itemType.ClassName))
                {
                    continue;
                }

                Type t = Type.GetType(itemType.ClassName + "," + itemType.AssemblyName, true);

                if (t.BaseType == typeof(System.Enum))
                {
                    str.AppendLine($"enum {GetModelName(t.FullName, t.Name)}");
                    str.AppendLine("{");

                    foreach (var f in t.GetFields(BindingFlags.Static | BindingFlags.Public))
                    {
                        int value = Convert.ToInt32(Enum.Parse(t, f.Name));
                        str.AppendLine($"{f.Name}={value},");
                    }

                    str.AppendLine("}");
                    str.AppendLine();
                }
                else
                {
                    str.AppendLine($"struct {GetModelName(t.FullName, t.Name)}");
                    str.AppendLine("{");

                    int i = 1;
                    foreach (var p in t.GetProperties())
                    {
                        str.AppendLine($"{i++}: optional {GetThriftType(p.PropertyType.ToString())} {p.Name}");
                    }

                    str.AppendLine("}");
                    str.AppendLine();
                }
            }

            str.AppendLine($"service {serviceName}");
            str.AppendLine("{");


            foreach (FunInfo f in funs)
            {
                f.CanReturnNull = CanReturnNull(f.ReturnType);
                str.Append(GetThriftType(f.ReturnType) + " ");
                str.Append(f.FunName + "(");

                for (int i = 0; i < f.Parameters.Count; i++)
                {
                    str.Append($"{i + 1}:");
                    str.Append(GetThriftType(f.Parameters[i].Item1) + " ");
                    str.Append(f.Parameters[i].Item2);
                    if (i < f.Parameters.Count - 1)
                    {
                        str.Append(",");
                    }
                }
                str.Append(")");
                str.AppendLine();
            }

            str.AppendLine("}");

            ConsoleColor currentForeColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("IDL模板:");
            Console.WriteLine();
            Console.WriteLine(str.ToString());
            Console.WriteLine();
            Console.ForegroundColor = currentForeColor;

            return(Tuple.Create(Namespace, serviceName, str.ToString()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成IDL模板
        /// </summary>
        /// <param name="type"></param>
        public Tuple <string, string, string> Create(Type[] types, string assemblyName)
        {
            List <Type> serviceTypeList = new List <Type>();

            foreach (var type in types)
            {
                if (type.IsAbstract || type.IsInterface)
                {
                    serviceTypeList.Add(type);
                }
            }

            List <TypeInfo> typeList = new List <TypeInfo>(); //实体类型集合
            Dictionary <Type, List <FunInfo> > funsDic = new Dictionary <Type, List <FunInfo> >();

            foreach (var ifaceType in serviceTypeList)
            {
                List <FunInfo> funs = new List <FunInfo>();
                foreach (MethodInfo m in ifaceType.GetMethods())
                {
                    if (IsSystemMethod(m.Name))
                    {
                        continue;
                    }
                    FunInfo fun = new FunInfo();

                    fun.ReturnType = m.ReturnType.ToString();
                    fun.FunName    = m.Name;

                    fun.Parameters = new List <Tuple <string, string> >();

                    string asName = m.ReturnType.Assembly.FullName.Substring(0, m.ReturnType.Assembly.FullName.IndexOf(","));

                    AddType(typeList, m.ReturnType.FullName, asName, m.ReturnType.BaseType == typeof(System.Enum));

                    foreach (var p in m.GetParameters())
                    {
                        fun.Parameters.Add(Tuple.Create(p.ParameterType.ToString(), p.Name));

                        string asPName = p.ParameterType.Assembly.FullName.Substring(0, p.ParameterType.Assembly.FullName.IndexOf(","));

                        AddType(typeList, p.ParameterType.FullName, asPName, p.ParameterType.BaseType == typeof(System.Enum));
                    }
                    funs.Add(fun);
                }
                funsDic.Add(ifaceType, funs);
            }

            typeList.Sort((x, y) => { return(y.IsEnum.CompareTo(x.IsEnum)); });


            //--------------------------------------------------//

            System.Text.StringBuilder str = new StringBuilder();

            str.AppendLine($"namespace csharp   {assemblyName}");
            str.AppendLine($"namespace java {assemblyName}");
            str.AppendLine($"namespace cpp  {assemblyName}");

            str.AppendLine();

            List <Tuple <string, string> > listType = new List <Tuple <string, string> >();

            foreach (var itemType in typeList)
            {
                if (isSystemType(itemType.ClassName))
                {
                    continue;
                }

                Type t = Type.GetType(itemType.ClassName + "," + itemType.AssemblyName, true);

                if (listType.Exists(x => x.Item2 == t.Name && x.Item1 != t.FullName))
                {
                    throw new Exception("简单类名不允许重复");
                }
                listType.Add(Tuple.Create <string, string>(t.FullName, t.Name));

                if (t.BaseType == typeof(System.Enum))
                {
                    str.AppendLine($"enum {t.Name}");
                    str.AppendLine("{");

                    foreach (var f in t.GetFields(BindingFlags.Static | BindingFlags.Public))
                    {
                        int value = Convert.ToInt32(Enum.Parse(t, f.Name));
                        str.AppendLine($"{f.Name}={value},");
                    }

                    str.AppendLine("}");
                    str.AppendLine();
                }
                else
                {
                    str.AppendLine($"struct { t.Name}");
                    str.AppendLine("{");

                    int i = 1;
                    foreach (var p in t.GetProperties())
                    {
                        str.AppendLine($"{i++}: optional {GetThriftType(p.PropertyType.ToString())} {p.Name}");
                    }

                    str.AppendLine("}");
                    str.AppendLine();
                }
            }
            foreach (var ifaceType in serviceTypeList)
            {
                str.AppendLine($"service {ifaceType.Name}");
                str.AppendLine("{");

                var funs = funsDic[ifaceType];
                foreach (FunInfo f in funs)
                {
                    f.CanReturnNull = CanReturnNull(f.ReturnType);
                    str.Append(GetThriftType(f.ReturnType) + " ");
                    str.Append(f.FunName + "(");

                    for (int i = 0; i < f.Parameters.Count; i++)
                    {
                        str.Append($"{i + 1}:");
                        str.Append(GetThriftType(f.Parameters[i].Item1) + " ");
                        str.Append(f.Parameters[i].Item2);
                        if (i < f.Parameters.Count - 1)
                        {
                            str.Append(",");
                        }
                    }
                    str.Append(")");
                    str.AppendLine();
                }

                str.AppendLine("}");
                str.AppendLine();
            }

            ConsoleColor currentForeColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("IDL模板:");
            Console.WriteLine();
            Console.WriteLine(str.ToString());
            Console.WriteLine();
            Console.ForegroundColor = currentForeColor;

            return(Tuple.Create(assemblyName, string.Empty, str.ToString()));
        }