using Nancy.Swagger; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace 电子展板.Utility.Swagger { public static class SwaggerModelDataUtils { public static SwaggerModelData GetModelData() { return SwaggerModelData.ForType(with => { Type type = typeof(T); SwaggerClassCommentAttribute classComment = type.GetCustomAttribute(); with.Description(classComment != null ? classComment.Comment : type.Name); PropertyInfo[] propertyInfos = typeof(T).GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { SwaggerModelPropertyData ss = with.Data.Properties.Where(x => x.Name == propertyInfo.Name).FirstOrDefault(); SwaggerCommentAttribute attribute = propertyInfo.GetCustomAttribute(); if (attribute == null) { continue; } if (!string.IsNullOrEmpty(attribute.Comment)) ss.Description = attribute.Comment; if (attribute.IsRequired != null) ss.Required = attribute.IsRequired.Value; if (attribute.Min != null) ss.Minimum = attribute.Min.Value; if (attribute.Max != null) ss.Maximum = attribute.Max.Value; } }); } } }