C # hide Attribute in a derived class

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

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

// hide the Authorize attribute
public class ErrorController : ApplicationController
{
}
+5
source share
5 answers

You can override AuthorizeAttribute with your own class and specify that it not be inherited.

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

Now you can specify which class to use if you have ApplicationController.

+2
source

, , . , , , .

, , , . , (has-a is-a) , ; .

+3

/prop, (new) . .

public new SomeType Foo() { return base.Foo(); }
+2

, "Hide". :

// hide the Authorize attribute
[Authorize(Roles = "")]
public class ErrorController : ApplicationController
{
}
+1

AttributeUage Attribute, :

[AttributeUsage(AttributeTargets.Class, Inherited=false)]
public class AuthorizeAttribute : Attribute
{
}

Then classes that are derived from the class in which you applied the attribute will not inherit the attribute.

Ow, now I understand that the Authorize attribute is not a custom attribute.

0
source

All Articles