Given the following example
class BaseProperty
{
}
class DerivedProperty : BaseProperty
{
}
abstract class BaseWithAbstractProperty
{
public abstract BaseProperty Property { get; }
public virtual BaseProperty OtherProperty { get; }
}
sealed class DerivedWithCovariantOverride : BaseWithAbstractProperty
{
public override DerivedProperty Property { get; }
public override BaseProperty OtherProperty { get; }
}
How do I determine that property Property
of class DerivedWithCovariantOverride
is an override of the abstract
property with the same name on class BaseWithAbstractProperty
? With OtherProperty
, the following will return true
var otherProperty = typeof(BaseWithAbstractProperty).GetProperty("OtherProperty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
var derivedProperty = typeof(DerivedWithCovariantOverride).GetProperty("OtherProperty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
Console.WriteLine(derivedProperty.GetMethod.GetBaseDefinition() == otherProperty.GetMethod);
But with the covariant override, the following will return false
var baseProperty = typeof(BaseWithAbstractProperty).GetProperty("Property", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
var covariantProperty = typeof(DerivedWithCovariantOverride).GetProperty("Property", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
Console.WriteLine(covariantProperty.GetMethod.GetBaseDefinition() == baseProperty.GetMethod);
The only special thing I see is that the GetMethod
on the covariant property returns a MethodInfo
which has attribute PreserveBaseOverridesAttribute
. Looking at the docs and the dotnet Github repo, it's clearly related to this covariant behavior. But all in all, it almost looks like that property is not a normal override.