这会导致编译时异常:
public sealed class ValidatesAttribute<T> : Attribute
{
}
[Validates<string>]
public static class StringValidation
{
}
我意识到c#不支持泛型属性。然而,在谷歌搜索了很多次之后,我似乎找不到原因。
有人知道为什么泛型类型不能从Attribute派生吗?什么理论吗?
这会导致编译时异常:
public sealed class ValidatesAttribute<T> : Attribute
{
}
[Validates<string>]
public static class StringValidation
{
}
我意识到c#不支持泛型属性。然而,在谷歌搜索了很多次之后,我似乎找不到原因。
有人知道为什么泛型类型不能从Attribute派生吗?什么理论吗?
当前回答
我的变通方法是这样的:
public class DistinctType1IdValidation : ValidationAttribute
{
private readonly DistinctValidator<Type1> validator;
public DistinctIdValidation()
{
validator = new DistinctValidator<Type1>(x=>x.Id);
}
public override bool IsValid(object value)
{
return validator.IsValid(value);
}
}
public class DistinctType2NameValidation : ValidationAttribute
{
private readonly DistinctValidator<Type2> validator;
public DistinctType2NameValidation()
{
validator = new DistinctValidator<Type2>(x=>x.Name);
}
public override bool IsValid(object value)
{
return validator.IsValid(value);
}
}
...
[DataMember, DistinctType1IdValidation ]
public Type1[] Items { get; set; }
[DataMember, DistinctType2NameValidation ]
public Type2[] Items { get; set; }
其他回答
这是一个非常好的问题。根据我使用属性的经验,我认为约束是合适的,因为当反射一个属性时,它会创建一个条件,在这种条件下,您必须检查所有可能的类型排列:typeof(Validates<string>), typeof(Validates<SomeCustomType>),等等……
在我看来,如果需要根据类型进行自定义验证,那么属性可能不是最好的方法。
也许采用SomeCustomValidationDelegate或ISomeCustomValidator作为参数的验证类是更好的方法。
这并不是真正的泛型,您仍然需要为每个类型编写特定的属性类,但是您可以使用泛型基本接口来编写一些防御性的代码,编写比其他需要的代码更少的代码,获得多态性的好处等。
//an interface which means it can't have its own implementation.
//You might need to use extension methods on this interface for that.
public interface ValidatesAttribute<T>
{
T Value { get; } //or whatever that is
bool IsValid { get; } //etc
}
public class ValidatesStringAttribute : Attribute, ValidatesAttribute<string>
{
//...
}
public class ValidatesIntAttribute : Attribute, ValidatesAttribute<int>
{
//...
}
[ValidatesString]
public static class StringValidation
{
}
[ValidatesInt]
public static class IntValidation
{
}
泛型属性从c# 11开始可用。现在,这是可能的:
[GenericAttribute<int>()]
public int Method();
然而,这还不可能:
[GenericAttribute<T>()]
public int Method<T>(T param);
T在编译时是未知的。
同时,
The type arguments must satisfy the same restrictions as the typeof operator. Types that require metadata annotations aren't allowed. For example, the following types aren't allowed as the type parameter: dynamic string? (or any nullable reference type) (int X, int Y) (or any other tuple types using C# tuple syntax). These types aren't directly represented in metadata. They include annotations that describe the type. In all cases, you can use the underlying type instead: object for dynamic. string instead of string?. ValueTuple<int, int> instead of (int X, int Y).
来源:https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11 generic-attributes
属性在编译时修饰类,但泛型类直到运行时才接收其最终类型信息。因为属性会影响编译,所以它必须在编译时是“完整的”。
有关更多信息,请参阅MSDN文章。
我的变通方法是这样的:
public class DistinctType1IdValidation : ValidationAttribute
{
private readonly DistinctValidator<Type1> validator;
public DistinctIdValidation()
{
validator = new DistinctValidator<Type1>(x=>x.Id);
}
public override bool IsValid(object value)
{
return validator.IsValid(value);
}
}
public class DistinctType2NameValidation : ValidationAttribute
{
private readonly DistinctValidator<Type2> validator;
public DistinctType2NameValidation()
{
validator = new DistinctValidator<Type2>(x=>x.Name);
}
public override bool IsValid(object value)
{
return validator.IsValid(value);
}
}
...
[DataMember, DistinctType1IdValidation ]
public Type1[] Items { get; set; }
[DataMember, DistinctType2NameValidation ]
public Type2[] Items { get; set; }