Compiler Error CS0592
Attribute 'attribute' is not valid on this declaration type. It is valid on 'type' declarations only.
When you define an attribute, you define what constructs it can be applied to by specifying an AttributeTargets
value. In the following example, the MyAttribute
attribute can be applied to interfaces only, because the AttributeUsage
attribute specifies AttributeTargets.Interface
. The error is generated because the attribute is applied to a class (class A
).
Example
The following sample generates CS0592:
// CS0592.cs
using System;
[AttributeUsage(AttributeTargets.Interface)]
public class MyAttribute : Attribute
{
}
[MyAttribute]
// Generates CS0592 because MyAttribute is not valid for a class.
public class A
{
public static void Main()
{
}
}
See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for