Why should Custom Validation Attributes be sealed in C#

154 views Asked by At

I'm trying to create a custom C# Validation Attribute(without sealed keyword) for Data Annotation.

 public class MyValidatorAttribute : ValidationAttribute
 {
 }

But Microsoft page here says "Make the class not inheritable." why?

Do I overlooked something by allowing "MyValidatorAttribute" class as inheritable?

1

There are 1 answers

0
Richard Garside On

According to this code analysis rule it is for performance reasons.

When retrieving custom attributes .NET will normally search the inheritance hierarchy and this can add an overhead that may not be required.

This would not stop you creating an abstract attribute that several sealed implementations inherited from.