コード例 #1
0
        private ReqAndRes GetParamInfo(Type t)
        {
            if (t == null)
            {
                return(new ReqAndRes());
            }
            ReqAndRes rp         = new ReqAndRes();
            var       properties = t.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);

            foreach (var pro in properties)
            {
                //去掉非proto特性的属性
                if (pro.CustomAttributes.Count(ct => ct.AttributeType == typeof(ProtoMemberAttribute) ||
                                               ct.AttributeType == typeof(ProtoContractAttribute)) ==
                    0)
                {
                    continue;
                }

                ParamItems pi         = new ParamItems();
                var        methodName = pro.Name;
                var        returnType = GetListTypeName(pro.PropertyType);
                rp.ClassName        = t.Name;
                rp.ClassDes         = _xmlFilter.GetTypeDec(t);
                pi.Param            = methodName;
                pi.ParamTypeName    = returnType;
                pi.ParamTypeNameDec = _xmlFilter.GetMethodDec(pro);

                //默认都是必须的字段
                var isR = pro.CustomAttributes
                          .FirstOrDefault(p => p.AttributeType == typeof(ProtoMemberAttribute))
                          ?.NamedArguments
                          .FirstOrDefault(p => p.MemberName == "IsRequired").TypedValue.Value;
                pi.IsRequired = (bool?)isR ?? true;

                if (pro.PropertyType.IsUserClass())
                {
                    if (pro.PropertyType.GetTypeInfo().IsGenericType)
                    {
                        var tt = pro.PropertyType.GetGenericArguments();
                        pi.ClassObj = GetParamInfo(tt.First());
                    }
                    else if (pro.PropertyType.GetTypeInfo().IsEnum)
                    {
                        pi.ClassObj = GetEnumInfo(pro, pro.PropertyType);
                    }
                    else
                    {
                        pi.ClassObj = GetParamInfo(pro.PropertyType);
                    }
                }

                rp.ParamItems.Add(pi);
            }
            return(rp);
        }
コード例 #2
0
        private ReqAndRes GetEnumInfo(PropertyInfo p, Type t)
        {
            ReqAndRes rp          = new ReqAndRes();
            var       fieldInfos  = t.GetFields(BindingFlags.Public | BindingFlags.Static);
            var       memberInfos = t.GetMembers(BindingFlags.Public | BindingFlags.Static);

            foreach (var field in fieldInfos)
            {
                ParamItems pi = new ParamItems();
                rp.ClassName        = t.Name;
                rp.ClassDes         = _xmlFilter.GetTypeDec(t);
                pi.Param            = field.Name;
                pi.ParamTypeName    = ((int)field.GetValue(p)).ToString();
                pi.ParamTypeNameDec = _xmlFilter.GetEnumDesc(field.FieldType, field.Name);
                rp.ParamItems.Add(pi);
            }
            return(rp);
        }