Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IEnumerable <ApiDescriptionEntity> GetApis()
        {
            /**
             *
             * ref: https://msdn.microsoft.com/en-us/library/system.reflection.assembly.definedtypes(v=vs.110).aspx
             * The DefinedTypes property is comparable to the Assembly.GetTypes method,
             * except that the DefinedTypes property returns a collection of TypeInfo objects,
             * and the Assembly.GetTypes method returns an array of Type objects.
             *
             * */
#if NET40
            var apis = this.assembly.GetTypes()
                       .Select(dt => dt.GetMethods().Select(method => new TypeAndMethod
            {
                TypeValue       = dt,
                MethodValue     = method,
                MethodAttribute = method.GetCustomAttributes <ApiMethodMemberAttribute>(true).FirstOrDefault()
            }))
                       .Aggregate(new List <TypeAndMethod>(), (pre, next) =>
            {
                pre.AddRange(next);
                return(pre);
            })
                       .Where(combined => combined.MethodAttribute != null)
                       .Select(combined => ApiDescriptionEntity.Init(
                                   combined.MethodAttribute.HttpMethod,
                                   combined.MethodAttribute.RelativeUrl,
                                   $"{combined.TypeValue.FullName}.{combined.MethodValue.Name}({String.Join(",", combined.MethodValue.GetParameters().Select(para => para.ParameterType.FullName))})",
                                   combined.MethodValue.GetParameters().Select(para => ApiParaDescEntity.Init(para.Name, para.ParameterType.FullName))
                                   ))
                       .ToList();
#else
            var apis = this.assembly.DefinedTypes
                       .Select(dt => dt.DeclaredMethods.Select(method => new TypeAndMethod
            {
                TypeValue       = dt,
                MethodValue     = method,
                MethodAttribute = method.GetCustomAttributes <ApiMethodMemberAttribute>(true).FirstOrDefault()
            }))
                       .Aggregate(new List <TypeAndMethod>(), (pre, next) =>
            {
                pre.AddRange(next);
                return(pre);
            })
                       .Where(combined => combined.MethodAttribute != null)
                       .Select(combined => ApiDescriptionEntity.Init(
                                   combined.MethodAttribute.HttpMethod,
                                   combined.MethodAttribute.RelativeUrl,
                                   $"{combined.TypeValue.FullName}.{combined.MethodValue.Name}({String.Join(",", combined.MethodValue.GetParameters().Select(para => para.ParameterType.FullName))})",
                                   combined.MethodValue.GetParameters().Select(para => ApiParaDescEntity.Init(para.Name, para.ParameterType.FullName))
                                   ))
                       .ToList();
#endif

            return(apis);
        }
        public IEnumerable <ApiDescriptionEntity> GetApis()
        {
            var query = from api in System.Web.Http.GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions
                        select ApiDescriptionEntity.Init(
                api.HttpMethod.ToString(),
                api.RelativePath,
                $"{api.ActionDescriptor.ControllerDescriptor.ControllerType}.{api.ActionDescriptor.ActionName}({String.Join(",", api.ActionDescriptor.GetParameters().Select(para => para.ParameterType.FullName))})",
                api.ActionDescriptor.GetParameters().Select(param => ApiParaDescEntity.Init(param.ParameterName, param.ParameterType.FullName))
                );

            return(query);
        }