Page 1 of 1

Warum kann ich keine geschützte Standard -Schnittstellenmethode aus einer Implementierungsklasse in C#aufrufen?

Posted: 02 Jun 2025, 17:19
by Anonymous
Ich versuche, eine geschützte Standardmethode in einer C# -Schinschnittstelle zu verwenden, aber ich kann sie nicht von meiner Implementierungsklasse aufrufen. Zum Beispiel: < /p>
interface IInterface
{
protected void Protected() { }
}

class Class : IInterface
{
public Class()
{
this.Protected(); // Fails with: 'Class' does not contain a definition for 'Protected' and no accessible extension method 'Protected' accepting a first argument of type 'Class' could be found (are you missing a using directive or an assembly reference?)
((IInterface)this).Protected(); // Fails with: Cannot access protected member 'IInterface.Protected()' via a qualifier of type 'IInterface'; the qualifier must be of type 'Class' (or derived from it)
}
}
< /code>
Sowohl direkte als auch explizite Anrufe werden nicht kompiliert. Warum ist das und wie kann ich die geschützte Standardimplementierung aus meiner Klasse aufrufen? Gibt es ein Problemumgehung oder ein empfohlenes Muster für dieses Szenario? Ich habe eine Situation, in der ich dies als abstrakte Basisklasse nicht tun kann, und muss dies für Fälle haben, in denen die Methode nicht implementiert wird.