这会导致编译时异常:
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派生吗?什么理论吗?
当前回答
属性在编译时修饰类,但泛型类直到运行时才接收其最终类型信息。因为属性会影响编译,所以它必须在编译时是“完整的”。
有关更多信息,请参阅MSDN文章。
其他回答
泛型属性从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
这目前还不是c#语言的特性,但是关于官方的c#语言回购有很多讨论。
以下是一些会议记录:
尽管这在原则上可行,但大多数都存在漏洞 运行时的版本,使它不能正常工作(它是 没有执行)。 我们需要一种机制来理解它工作在哪个目标运行时上。我们 很多事情都需要它,目前正在考虑。直到 那么,我们就不能接受了。 一个主要的c#版本的候选,如果我们能做出足够的数量 的运行时版本处理它。
我的变通方法是这样的:
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; }
这并不是真正的泛型,您仍然需要为每个类型编写特定的属性类,但是您可以使用泛型基本接口来编写一些防御性的代码,编写比其他需要的代码更少的代码,获得多态性的好处等。
//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
{
}
我不知道为什么不允许,但这是一个可行的解决办法
[AttributeUsage(AttributeTargets.Class)]
public class ClassDescriptionAttribute : Attribute
{
public ClassDescriptionAttribute(Type KeyDataType)
{
_KeyDataType = KeyDataType;
}
public Type KeyDataType
{
get { return _KeyDataType; }
}
private Type _KeyDataType;
}
[ClassDescriptionAttribute(typeof(string))]
class Program
{
....
}