Explicit interface implementation cannot be virtual

后端 未结 5 1736
遥遥无期
遥遥无期 2021-02-05 13:20

For the record, I\'ve already seen this connect item but I can\'t really understand what would be the problem in supporting this.

Say I have the following code:

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 13:41

    A method implementating interface explicitly has a special visibility scope = you cannot acces it from another method unless you cast "this" to the target interface type. I suppose it was the reason why virtual specifier is not supported - you cannot override method that is not part of the normal object interface (private/protected/public).

    This is my workaround:

    public class Base : IInterface
    {    
       protected virtual void Method()
       {
    
       }
    
       void IInterface.Method()    
       {        
           this.Method()
       }
     }
    
    
     public class Derived : Base
     {
         protected override void Method()
         {
         }
     }
    

提交回复
热议问题