本文整理汇总了C#中System.Reflection.PropertyInfo.GetAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetAttributes方法的具体用法?C# PropertyInfo.GetAttributes怎么用?C# PropertyInfo.GetAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetAttributes方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parameter
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);
}
}
示例2: GetValidationErrors
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>();
}
示例3: GetValidationErrors
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>();
}
示例4: PropertyMapper
/// <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();
}
示例5: CreateSwaggerModelPropertyData
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;
}
示例6: ShouldValidate
/// <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();
}
示例7: GetColumnType
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;
}
示例8: GetPrimaryKey
internal static PrimaryKeyAttribute GetPrimaryKey(PropertyInfo prop)
{
return prop.GetAttributes<PrimaryKeyAttribute>().FirstOrDefault();
}
示例9: GetIsAutoIncrement
internal static bool GetIsAutoIncrement(PropertyInfo prop)
{
return prop.GetAttributes<AutoIncrementAttribute>().Any();
}
示例10: GetUnique
internal static UniqueAttribute GetUnique(PropertyInfo prop)
{
return prop.GetAttributes<UniqueAttribute>().FirstOrDefault();
}
示例11: GetChecks
internal static string[] GetChecks(PropertyInfo prop)
{
return prop.GetAttributes<CheckAttribute>().Select(x => x.Expression).ToArray();
}
示例12: GetDataConverter
internal static DataConverterAttribute GetDataConverter(PropertyInfo prop)
{
return prop.GetAttributes<DataConverterAttribute>().FirstOrDefault();
}
示例13: GetIsColumnNullable
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();
}