Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Reflection;
- using System.Collections;
- using System.Collections.Generic;
- namespace Documentation
- {
- public class Specifier<T> : ISpecifier
- {
- private Type type = typeof(T);
- public string GetApiDescription()
- {
- return type
- .GetCustomAttributes(false)
- .OfType<ApiDescriptionAttribute>()
- .FirstOrDefault()?.Description;
- }
- public string[] GetApiMethodNames()
- {
- return type.GetMethods()
- .Where(x => x.GetCustomAttributes<ApiMethodAttribute>()
- .Any())
- .Select(y => y.Name).ToArray();
- }
- public string GetApiMethodDescription(string methodName)
- {
- return type
- .GetMethod(methodName)?
- .GetCustomAttributes(false)
- .OfType<ApiDescriptionAttribute>()
- .FirstOrDefault()?.Description;
- }
- public string[] GetApiMethodParamNames(string methodName)
- {
- return type
- .GetMethod(methodName)?
- .GetParameters()
- .Select(x => x.Name)
- .ToArray();
- }
- public string GetApiMethodParamDescription(string methodName, string paramName)
- {
- return type
- .GetMethod(methodName)?
- .GetParameters()
- .Where(x => x.Name == paramName)
- .FirstOrDefault()?
- .GetCustomAttributes(false)
- .OfType<ApiDescriptionAttribute>()
- .FirstOrDefault()?
- .Description;
- }
- public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName)
- {
- var allResults = new ApiParamDescription();
- var aid = paramName;
- AssistentForWork(paramName, methodName, allResults, aid);
- allResults.ParamDescription = new CommonDescription(
- paramName, GetApiMethodParamDescription(methodName, paramName));
- return allResults;
- }
- public ApiMethodDescription GetApiMethodFullDescription(string methodName)
- {
- var met = type.GetMethod(methodName);
- var result = new ApiMethodDescription();
- var paramDescription = new ApiParamDescription
- { ParamDescription = new CommonDescription() };
- var validAttr = met?.ReturnParameter?
- .GetCustomAttributes(false)
- .OfType<ApiIntValidationAttribute>()
- .FirstOrDefault();
- var reqAttr = met?
- .ReturnParameter?
- .GetCustomAttributes(false)
- .OfType<ApiRequiredAttribute>()
- .FirstOrDefault();
- if (met != null && !met
- .GetCustomAttributes(false)
- .OfType<ApiMethodAttribute>()
- .Any()) return null;
- result = GetDescriptionAssist(methodName, methodName.Trim(),
- paramDescription, validAttr, reqAttr);
- return result;
- }
- private void AssistentForWork(string param, string method, ApiParamDescription result, string assist)
- {
- var allInf = type.GetMethods()
- .FirstOrDefault(x => x.Name == method)?
- .GetParameters()
- .FirstOrDefault(y => y.Name == param);
- var validAttr = allInf?.GetCustomAttributes(false)
- .OfType<ApiIntValidationAttribute>()
- .FirstOrDefault();
- var reqAttr = allInf?.GetCustomAttributes(false)
- .OfType<ApiRequiredAttribute>()
- .FirstOrDefault();
- if (reqAttr != null && assist != null)
- result.Required = reqAttr.Required;
- if (validAttr != null && assist != null)
- {
- result.MaxValue = validAttr.MaxValue;
- result.MinValue = validAttr.MinValue;
- }
- }
- private ApiMethodDescription GetDescriptionAssist
- (string methodName, string assist,
- ApiParamDescription paramDescr, ApiIntValidationAttribute validAttr, ApiRequiredAttribute reqAttr)
- {
- var result = new ApiMethodDescription();
- if (assist != null)
- result = new ApiMethodDescription
- {
- MethodDescription = new CommonDescription(methodName,
- GetApiMethodDescription(methodName)),
- ParamDescriptions = GetApiMethodParamNames(methodName)
- .Select(x => GetApiMethodParamFullDescription(methodName, x))
- .ToArray()
- };
- if (validAttr != null && assist != null)
- {
- paramDescr.MaxValue = validAttr.MaxValue;
- paramDescr.MinValue = validAttr.MinValue;
- }
- if (assist != null && reqAttr != null)
- {
- paramDescr.Required = reqAttr.Required;
- result.ReturnDescription = paramDescr;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment