I saw a few abstract base classes .NET provides and I'm completely lost the way their constructors and virtual methods defined. For example, see System.ComponentModel.DataAnnotations.ValidationAttribute base class:
public abstract class ValidationAttribute : Attribute
{
// Summary:
// Initializes a new instance of the System.ComponentModel.DataAnnotations.ValidationAttribute
// class.
protected ValidationAttribute();
...
protected virtual ValidationResult IsValid(object value, ValidationContext validationContext);
}
But I can't do this in my own class:
public abstract class MyClass
{
protected MyClass();
}
MyClass.MyClass() must declare a body because it's not marked abstract, extern, or partial
And I can't mark it abstract either:
The modifier abstract is not valid for this item
I have no specific need MyClass to be like ValidationAttribute or other base classes in .NET. I just want to know how did they compile on the runtime.
You are not looking at the code of ValidationAttribute, you are looking at its metadata.
The actual constructor looks like
protected ValidationAttribute()
: this(() => DataAnnotationsResources.ValidationAttribute_ValidationError) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With