I have a class:
public abstract class InvestorAccount
{
public bool IsRegistered => ...;
}
And a subclass:
public class PrivateInvestorAccount : InvestorAccount
{
public Person? Holder { get; set; }
}
I would like to teach the compiler that Holder is not null when IsRegistered is true.
If both props were declared in the same class if could just annotate IsRegistered with [MemberNotNullWhen(true, nameof(Holder))]. However, in the inheritance scenario, applying
[MemberNotNullWhen(true, nameof(PrivateInvestorAccount.Holder))] ends up in a compilation error:
error CS8776: Member 'Holder' cannot be used in this attribute.
Is there any way of telling the compiler that Holder is not null in this scenario?