为.NET属性目标指定所需的基类

时间:2022-11-27 14:30:39

I tried to create a custom .NET attribute with the code below but accidentally left off the subclass. This generated an easily-fixed compiler error shown in the comment.

我尝试使用下面的代码创建自定义.NET属性,但不小心将子类留下了。这会在注释中生成一个容易修复的编译器错误。

// results in compiler error CS0641: Attribute 'AttributeUsage' is 
// only valid on classes derived from System.Attribute
[AttributeUsage(AttributeTargets.Class)]
internal class ToolDeclarationAttribute
{
    internal ToolDeclarationAttribute()
    {
    }
}

My question is how does the compiler know the [AttributeUsage] attribute can only be applied to a subclass of System.Attribute? Using .NET Reflector I don't see anything special on the AttributeUsageAttribute class declaration itself. Unfortunately this might just be a special case generated by the compiler itself.

我的问题是编译器如何知道[AttributeUsage]属性只能应用于System.Attribute的子类?使用.NET Reflector我没有在AttributeUsageAttribute类声明本身上看到任何特殊内容。不幸的是,这可能只是编译器本身生成的一种特殊情况。

[Serializable, ComVisible(true), AttributeUsage(AttributeTargets.Class, Inherited=true)]
public sealed class AttributeUsageAttribute : Attribute
{
    ...

I would like to be able to specify that my custom attribute can only be placed on subclasses of a particular class (or interface). Is this possible?

我希望能够指定我的自定义属性只能放在特定类(或接口)的子类上。这可能吗?

3 个解决方案

#1


I would like to be able to specify that my custom attribute can only be placed on subclasses of a particular class (or interface). Is this possible?

我希望能够指定我的自定义属性只能放在特定类(或接口)的子类上。这可能吗?

Actually, there is a way to do this for subclasses (but not interfaces) using protected - see Restricting Attribute Usage. To reproduce the code (but not the discussion):

实际上,有一种方法可以使用protected来为子类(但不是接口)执行此操作 - 请参阅限制属性使用。要重现代码(但不是讨论):

abstract class MyBase {
    [AttributeUsage(AttributeTargets.Property)]
    protected sealed class SpecialAttribute : Attribute {}
}
class ShouldBeValid : MyBase {
    [Special] // works fine
    public int Foo { get; set; }
}
class ShouldBeInvalid { // not a subclass of MyBase
    [Special] // type or namespace not found
    [MyBase.Special] // inaccessible due to protection level
    public int Bar{ get; set; }
}

#2


AttributeUsageAttribute is just a magic class (like Attribute itself is). This is a built-in compiler rule, and you cannot do something like that for your own attributes.

AttributeUsageAttribute只是一个魔术类(就像属性本身一样)。这是一个内置的编译器规则,你不能为自己的属性做类似的事情。

#3


With ReSharper you can use [JetBrains.Annotations.BaseTypeRequired(typeof(YouBaseType))]

使用ReSharper,您可以使用[JetBrains.Annotations.BaseTypeRequired(typeof(YouBaseType))]

#1


I would like to be able to specify that my custom attribute can only be placed on subclasses of a particular class (or interface). Is this possible?

我希望能够指定我的自定义属性只能放在特定类(或接口)的子类上。这可能吗?

Actually, there is a way to do this for subclasses (but not interfaces) using protected - see Restricting Attribute Usage. To reproduce the code (but not the discussion):

实际上,有一种方法可以使用protected来为子类(但不是接口)执行此操作 - 请参阅限制属性使用。要重现代码(但不是讨论):

abstract class MyBase {
    [AttributeUsage(AttributeTargets.Property)]
    protected sealed class SpecialAttribute : Attribute {}
}
class ShouldBeValid : MyBase {
    [Special] // works fine
    public int Foo { get; set; }
}
class ShouldBeInvalid { // not a subclass of MyBase
    [Special] // type or namespace not found
    [MyBase.Special] // inaccessible due to protection level
    public int Bar{ get; set; }
}

#2


AttributeUsageAttribute is just a magic class (like Attribute itself is). This is a built-in compiler rule, and you cannot do something like that for your own attributes.

AttributeUsageAttribute只是一个魔术类(就像属性本身一样)。这是一个内置的编译器规则,你不能为自己的属性做类似的事情。

#3


With ReSharper you can use [JetBrains.Annotations.BaseTypeRequired(typeof(YouBaseType))]

使用ReSharper,您可以使用[JetBrains.Annotations.BaseTypeRequired(typeof(YouBaseType))]