简体   繁体   English

仅允许特定类型的自定义属性

[英]Allow a custom Attribute only on specific type

Is there a way to force the compiler to restrict the usage of a custom attribute to be used only on specific property types like int, short, string (all the primitive types)?有没有办法强制编译器限制自定义属性的使用,使其仅用于特定的属性类型,如 int、short、string(所有原始类型)?
similar to the AttributeUsageAttribute 's ValidOn- AttributeTargets enumeration.类似于AttributeUsageAttribute的 ValidOn- AttributeTargets枚举。

No, you can't, basically. 不,基本上不能。 You can limit it to struct vs class vs interface , that is about it. 您可以将它限制为struct vs class vs interface Plus: you can't add attributes to types outside your code anyway (except for via TypeDescriptor , which isn't the same). 另外:无论如何,您都不能向代码外部的类型添加属性(通过TypeDescriptor除外)。

You can run this unit test to check it. 您可以运行此单元测试以进行检查。

First, declare validation attribute PropertyType: 首先,声明验证属性PropertyType:

  [AttributeUsage(AttributeTargets.Class)]
    // [JetBrains.Annotations.BaseTypeRequired(typeof(Attribute))] uncomment if you use JetBrains.Annotations
    public class PropertyTypeAttribute : Attribute
    {
        public Type[] Types { get; private set; }

        public PropertyTypeAttribute(params Type[] types)
        {
            Types = types;
        }
    }

Create unit test: 创建单元测试:

 [TestClass]
    public class TestPropertyType 
    {
        public static Type GetNullableUnderlying(Type nullableType)
        {
            return Nullable.GetUnderlyingType(nullableType) ?? nullableType;
        }

        [TestMethod]
        public void Test_PropertyType()
        {
            var allTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes());
            var allPropertyInfos = allTypes.SelectMany(a => a.GetProperties()).ToArray();

            foreach (var propertyInfo in allPropertyInfos)
            {
                var propertyType = GetNullableUnderlying(propertyInfo.PropertyType);
                foreach (var attribute in propertyInfo.GetCustomAttributes(true))
                {
                    var attributes = attribute.GetType().GetCustomAttributes(true).OfType<PropertyTypeAttribute>();
                    foreach (var propertyTypeAttr in attributes)
                        if (!propertyTypeAttr.Types.Contains(propertyType))
                            throw new Exception(string.Format(
                                "Property '{0}.{1}' has invalid type: '{2}'. Allowed types for attribute '{3}': {4}",
                                propertyInfo.DeclaringType,
                                propertyInfo.Name,
                                propertyInfo.PropertyType,
                                attribute.GetType(),
                                string.Join(",", propertyTypeAttr.Types.Select(x => "'" + x.ToString() + "'"))));
                }
            }
        }
    }

Your attribute, for example allow only decimal property types: 例如,您的属性仅允许使用十进制属性类型:

 [AttributeUsage(AttributeTargets.Property)]
    [PropertyType(typeof(decimal))]
    public class PriceAttribute : Attribute
    {

    }

Example model: 示例模型:

public class TestModel  
{
    [Price]
    public decimal Price1 { get; set; } // ok

    [Price]
    public double Price2 { get; set; } // error
}

The code below will return an error if the attribute was placed on a property/field that is not List of string. 如果属性放置在非字符串列表的属性/字段中,则下面的代码将返回错误。

The line if (!(value is List<string> list)) may be a C#6 or 7 feature. if (!(value is List<string> list))可能是C#6或7功能。

[AttributeUsage(AttributeTargets.Property |
                AttributeTargets.Field, AllowMultiple = false)]
public sealed class RequiredStringListAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        if (!(value is List<string> list))
            return new ValidationResult($"The required attrribute must be of type List<string>");

        bool valid = false;
        foreach (var item in list)
        {
            if (!string.IsNullOrWhiteSpace(item))
                valid = true;
        }

        return valid
            ? ValidationResult.Success
            : new ValidationResult($"This field is required"); ;
    }

}

您可以自己编写代码以强制正确使用属性类,但是这可以做很多。

The way I am doing this is following:我这样做的方式如下:

[AttributeUsage(AttributeTargets.Property)]
public class SomeValidationAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value is not string stringToValidate)
        {
            throw new AttributeValueIsNotStringException(validationContext.DisplayName, validationContext.ObjectType.Name);
        }

        // validationContext.DisplayName is name of property, where validation attribut was used.
        // validationContext.ObjectType.Name is name of class, in which the property is placed to instantly identify, where is the error.
        
        //Some validation here.

        return ValidationResult.Success;
    }
}

And exception look like this:异常看起来像这样:

public class AttributeValueIsNotStringException : Exception
{
    public AttributeValueIsNotStringException(string propertyName, string className) : base(CreateMessage(propertyName, className))
    {

    }

    private static string CreateMessage(string propertyName, string className)
    {
        return $"Validation attribute cannot be used for property: \"{propertyName}\" in class: \"{className}\" because it's type is not string. Use it only for string properties.";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM