public MethodTemplate(MethodInfo method)
        {
            Method     = method;
            Name       = method.Name;
            IsAsync    = method.ReturnType.FullName.StartsWith(typeof(Task).FullName);
            ReturnType = new TypeTemplate(method.ReturnType);
            foreach (var attr in CustomAttributeData.GetCustomAttributes(method))
            {
                AttributeList.Add(new CustomAttributeTemplate(attr).ToString());
            }
            //方法参数
            foreach (var parameter in method.GetParameters())
            {
                Parameters.Add(new ParameterTemplate(parameter));
            }

            if (method.IsGenericMethod)
            {
                foreach (var argumentType in method.GetGenericArguments())
                {
                    var argument = new GenericArgumentTemplate()
                    {
                        Name = argumentType.Name
                    };
                    GenericArguments.Add(argument);

                    if (argumentType.GenericParameterAttributes == GenericParameterAttributes.None &&
                        argumentType.GetGenericParameterConstraints().Length == 0)
                    {
                        continue;
                    }

                    //class约束
                    if (argumentType.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
                    {
                        argument.Constraints.Add("class");
                    }
                    //值类型约束
                    if (argumentType.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint))
                    {
                        argument.Constraints.Add("struct");
                    }

                    foreach (var type in argumentType.GetGenericParameterConstraints())
                    {
                        argument.Constraints.Add(new TypeTemplate(type).ToString());
                    }

                    //无参构造函数
                    if (argumentType.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
                    {
                        argument.Constraints.Add("new()");
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// 模板数据生成
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        internal static TemplateMetadata BuildTemplate(Type[] types)
        {
            var template = new TemplateMetadata();

            template.UsingList.Add(typeof(object).Namespace);
            template.UsingList.Add(typeof(Thread).Namespace);
            template.UsingList.Add(typeof(Task).Namespace);
            template.UsingList.Add(typeof(IHttpService).Namespace);
            template.UsingList.Add(typeof(HttpServiceAttribute).Namespace);
            template.UsingList.Add(typeof(HttpClientWrapperExtension).Namespace);

            foreach (var type in types)
            {
                var classTemplate = new ClassTemplate()
                {
                    InterfaceType = type,
                    Namespace     = $"Dynamic.{RandomName()}",
                    Name          = $"AutoGenerated{type.Name.TrimStart('I').Replace("`", string.Empty)}"
                };
                classTemplate.UsingList.Add(type.Namespace);
                //类标签
                foreach (var attr in CustomAttributeData.GetCustomAttributes(type))
                {
                    classTemplate.AttributeList.Add(new CustomAttributeTemplate(attr).ToString());
                }
                //泛型类信息收集
                if (type.IsGenericType)
                {
                    foreach (var argumentType in type.GetGenericArguments())
                    {
                        var argument = new GenericArgumentTemplate()
                        {
                            Name = argumentType.Name
                        };
                        classTemplate.GenericArguments.Add(argument);

                        if (argumentType.GenericParameterAttributes == GenericParameterAttributes.None &&
                            argumentType.GetGenericParameterConstraints().Length == 0)
                        {
                            continue;
                        }

                        //class约束
                        if (argumentType.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
                        {
                            argument.Constraints.Add("class");
                        }
                        //值类型约束
                        if (argumentType.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint))
                        {
                            argument.Constraints.Add("struct");
                        }

                        //类型约束
                        foreach (var constraintType in argumentType.GetGenericParameterConstraints())
                        {
                            argument.Constraints.Add(new TypeTemplate(constraintType).ToString());
                        }

                        //无参构造函数
                        if (argumentType.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
                        {
                            argument.Constraints.Add("new()");
                        }
                    }
                }

                //方法信息
                foreach (var method in type.GetRuntimeMethods())
                {
                    if (method.IsSpecialName)
                    {
                        continue;
                    }
                    classTemplate.MethodList.Add(new MethodTemplate(method));
                }
                //属性
                foreach (var property in type.GetRuntimeProperties())
                {
                    if (property.IsSpecialName)
                    {
                        continue;
                    }
                    classTemplate.PropertyList.Add(new PropertyTemplate(property));
                }
                //todo:不支持事件
                template.ClassList.Add(classTemplate);
            }
            return(template);
        }