I have base class(CompareAttribute
) which implemented IClientValidatable
[AttributeUsage(AttributeTargets.Property)]
public class NotContainsAttribute : CompareAttribute
{
}
I want to override method IEnumerable<ModelClientValidationRule> GetClientValidationRules
but i can't do that because it is not virtual(cannot override inherited member, because it is not marked virtual, abstract, or override
).
So i just declare my own method in my NotContainsAttribute
class
public new IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
}
After i run the program all working like expected but i get warning in compile time that my class hides inherited member, (Warning "NotContainsAttribute" hides inherited member Use the new keyword if hiding was intended.
)
But if I use new
keyword
public new IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationNotContains (this.FormatErrorMessage(metadata.GetDisplayName()), CompareAttribute.FormatPropertyForClientValidation(this.OtherProperty));
yield break;
}
In such case my method not used, base class method used instead.
I want my method to be used, without new
keyword i get it used but why compiler saying that i need to use new
keyword if hiding was intended
?
I understand that if hiding was intended
means that if you want to hide base method and use your method insteard mark it with new keyword
but on practice it is really hiding my method.
May be some one may clarify that and is it good practice to declare method with the same name if that method in base class not allowed to be overridden but really you need to override it?