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 { }
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.
, , . , , , .
, , , . , (has-a is-a) , ; .
/prop, (new) . .
new
public new SomeType Foo() { return base.Foo(); }
, "Hide". :
// hide the Authorize attribute [Authorize(Roles = "")] public class ErrorController : ApplicationController { }
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.