Search code examples
c#asp.net-mvclanguage-design

c# hide Attribute in derived Class


I have a base class with an attribute and I want to hide it in a derived class. Is there any way to do this other than using reflection?

[Authorize(Roles = "User,Admin,Customs")]
public abstract class ApplicationController : Controller
{
}

// hide the Authorize attribute
public class ErrorController : ApplicationController
{
}

Solution

  • You could override the AuthorizeAttribute with your own class and specify it to not be inherited.

    [AttributeUsage(AttributeTargets.Class, Inherited=false)]
    public class NonInheritedAuthorizeAttribute : AuthorizeAttribute
    {
        // Constructors, etc.
    }
    

    Now you can specify which class to use, as long as you own ApplicationController.