Skip to main content

How to Resolve Error "CS0641: Attribute 'attribute' is only valid on classes derived from System.Attribute" in C#

The Compiler Error CS0641 is a restriction error regarding the definition of Custom Attributes. The message reads: "Attribute 'AttributeUsage' is only valid on classes derived from System.Attribute".

In C#, if you want to create your own attribute (e.g., [MyCustomAttribute]), you must define a class that inherits from System.Attribute. The [AttributeUsage] attribute is a special meta-attribute used to configure your custom attribute (defining where it can be used, whether it allows duplicates, etc.). Because its sole purpose is to configure other attributes, you are only allowed to place it on classes that are themselves attributes.

This guide explains how to properly define custom attributes.

Understanding Attribute Definition

To create a valid custom attribute in C#, two things are required:

  1. Inheritance: The class must inherit from System.Attribute.
  2. Naming Convention: The class name should end with the suffix "Attribute" (e.g., ValidationAttribute).

The [AttributeUsage] attribute is used to control the behavior of your new attribute class. If you try to put [AttributeUsage] on a standard class (like a User model or a Service), the compiler raises CS0641 because standard classes cannot act as attributes.

Scenario: Applying AttributeUsage Incorrectly

This error occurs when a developer tries to "tag" a normal class with usage rules, perhaps misunderstanding that AttributeUsage is for defining metadata tools, not for defining business logic.

Example of error: attempting to put AttributeUsage on a standard business class.

using System;

// ⛔️ Error CS0641: Attribute 'AttributeUsage' is only valid on classes derived from System.Attribute
[AttributeUsage(AttributeTargets.Class)]
public class UserProfile
{
public string Name { get; set; }
}

Solution: Inherit from System.Attribute

If your intention was indeed to create a custom attribute (metadata), you must inherit from the base class.

Solution

Add : Attribute (or : System.Attribute) to the class definition.

using System;

// ✅ Correct: 'UserMetadataAttribute' inherits from Attribute.
// Now 'AttributeUsage' is valid because it configures this new attribute definition.
[AttributeUsage(AttributeTargets.Class)]
public class UserMetadataAttribute : Attribute
{
public string Description { get; set; }
}

// Usage:
[UserMetadata(Description = "Admin User")]
public class UserProfile
{
}

Alternative Solution: Remove the Attribute

If your intention was just to create a normal class (like UserProfile in the mistake above) and you don't intend it to be metadata, simply remove the [AttributeUsage] line. It serves no purpose on a standard class.

// ✅ Correct: A standard class
public class UserProfile
{
public string Name { get; set; }
}

Conclusion

CS0641 ensures that meta-attributes are only applied to actual attributes.

  1. Check Inheritance: Does your class inherit from System.Attribute?
  2. If Yes: You are fine. Check for other syntax errors.
  3. If No:
    • If you want a custom attribute: Add : Attribute.
    • If you don't want a custom attribute: Remove [AttributeUsage].