public Parameter(PropertyInfo propertyInfo, IHasPositionerCounter context) { Examples = new List<ExampleAttribute>(); var attributes = propertyInfo.GetAttributes<DescriptionAttribute>(); if (attributes.Count == 1) { Description = attributes[0].Text; } var argumentAttribute = propertyInfo.GetAttributes<ArgumentAttribute>().Single(); if (argumentAttribute is NamedArgumentAttribute) { var namedArgumentAttribute = (NamedArgumentAttribute)argumentAttribute; Shorthand = namedArgumentAttribute.ShortHand; Name = namedArgumentAttribute.Name; } else if (argumentAttribute is PositionalArgumentAttribute) { Position = context.Position++; } PropertyInfo = propertyInfo; ArgumentAttribute = argumentAttribute; foreach (var example in propertyInfo.GetAttributes<ExampleAttribute>()) { Examples.Add(example); } }
private static IEnumerable<IError> GetValidationErrors(object instance, PropertyInfo property) { var validators = from attribute in property.GetAttributes<ValidationAttribute>(true) where !attribute.IsValid(property.GetValue(instance, null)) select new DefaultError( instance, property.Name, attribute.FormatErrorMessage(property.Name) ); return validators.OfType<IError>(); }
IEnumerable<Error> GetValidationErrors(object instance, PropertyInfo property) { var context = new ValidationContext(instance, null, null); var validators = from attribute in property.GetAttributes<ValidationAttribute>(true) where attribute.GetValidationResult(property.GetValue(instance, null), context) != ValidationResult.Success select new Error( instance, property.Name, attribute.FormatErrorMessage(property.Name) ); return validators.OfType<Error>(); }
/// <summary> /// 指定属性元数据,初始化一个 <see cref="PropertyMapper"/> 类的新实例。 /// </summary> /// <param name="typeMapper">类型的映射器。</param> /// <param name="property">成员的属性元数据。</param> public PropertyMapper(TypeMapper typeMapper, PropertyInfo property) : base(property) { if(typeMapper == null) throw new ArgumentNullException(nameof(typeMapper)); this.TypeMapper = typeMapper; this.IsIgnore = property.GetAttribute<IgnoreAttribute>() != null; this._LazyTypeDefaultValue = new Lazy<object>(property.PropertyType.GetDefaultValue); var aliasAttr = property.GetAttribute<IAliasAttribute>(); this.Name = aliasAttr != null && aliasAttr.Name != null ? aliasAttr.Name : property.Name; var keyAttr = property.GetAttribute<IKeyAttribute>(); this.IsKey = (keyAttr != null && keyAttr.IsKey) || string.Equals(property.Name, DbExtensions.DefaultKeyName, StringComparison.CurrentCultureIgnoreCase); this.Validators = property.GetAttributes<IPropertyValidator>().ToArray(); }
private SwaggerModelPropertyData CreateSwaggerModelPropertyData(PropertyInfo pi) { var modelProperty = new SwaggerModelPropertyData { Type = pi.PropertyType, Name = pi.Name }; foreach (var attr in pi.GetAttributes<SwaggerModelPropertyAttribute>()) { modelProperty.Name = attr.Name ?? modelProperty.Name; modelProperty.Description = attr.Description ?? modelProperty.Description; modelProperty.Minimum = attr.GetNullableMinimum() ?? modelProperty.Minimum; modelProperty.Maximum = attr.GetNullableMaximum() ?? modelProperty.Maximum; modelProperty.Required = attr.GetNullableRequired() ?? modelProperty.Required; modelProperty.UniqueItems = attr.GetNullableUniqueItems() ?? modelProperty.UniqueItems; modelProperty.Enum = attr.Enum ?? modelProperty.Enum; } return modelProperty; }
/// <summary> /// Indicates whether the specified property should be validated. /// </summary> /// <param name="property">The property.</param> /// <returns> /// true if should be validated; otherwise false /// </returns> public bool ShouldValidate(PropertyInfo property) { return property.GetAttributes<ValidationAttribute>(true).Any(); }
internal static Type GetColumnType(PropertyInfo prop) { Type nullableType = Nullable.GetUnderlyingType(prop.PropertyType); var type = nullableType ?? prop.PropertyType; DataConverterAttribute[] attrs = prop.GetAttributes<DataConverterAttribute>().ToArray(); if (attrs.Any()) { type = attrs.First().StorageType; } else if (type.GetTypeInfo().IsEnum) { var attribute = prop.GetAttributes<EnumAffinityAttribute>().FirstOrDefault(); type = attribute == null ? typeof (int) : attribute.Type; } return type; }
internal static PrimaryKeyAttribute GetPrimaryKey(PropertyInfo prop) { return prop.GetAttributes<PrimaryKeyAttribute>().FirstOrDefault(); }
internal static bool GetIsAutoIncrement(PropertyInfo prop) { return prop.GetAttributes<AutoIncrementAttribute>().Any(); }
internal static UniqueAttribute GetUnique(PropertyInfo prop) { return prop.GetAttributes<UniqueAttribute>().FirstOrDefault(); }
internal static string[] GetChecks(PropertyInfo prop) { return prop.GetAttributes<CheckAttribute>().Select(x => x.Expression).ToArray(); }
internal static DataConverterAttribute GetDataConverter(PropertyInfo prop) { return prop.GetAttributes<DataConverterAttribute>().FirstOrDefault(); }
internal static bool GetIsColumnNullable(PropertyInfo prop) { Type propertyType = prop.PropertyType; Type nullableType = Nullable.GetUnderlyingType(propertyType); return (nullableType != null || !propertyType.GetTypeInfo().IsValueType) && !prop.GetAttributes<NotNullAttribute>().Any(); }