类中的C#调用接口方法

0xD*_*EEF 3 c# interface class call

interface ILol
{
   void LOL();
}

class Rofl : ILol
{
   void ILol.LOL()
   {
      GlobalLOLHandler.RaiseROFLCOPTER(this);
   }
   public Rofl()
   {
      //Is there shorter way of writing this or i is there "other" problem with implementation??
      (this as ILol).LOL();
   }
}
Run Code Online (Sandbox Code Playgroud)

lad*_*dge 10

您已经明确地实现了接口,通常您不需要这样做.相反,只需隐式实现它,并像任何其他方法一样调用它:

class Rofl : ILol
{
    public void LOL() { ... }

    public Rofl()
    {
        LOL();
    }
}
Run Code Online (Sandbox Code Playgroud)

(注意您的实施也需要公开.)


Ste*_*art 9

您可能希望将演员阵容从更改(this as ILol)((ILol)this).允许as cast返回null,这可能导致以后混淆错误以及编译器必须测试的错误.


Mar*_*ers 5

不要使用as,只是施放:

((ILol)this).LOL();
Run Code Online (Sandbox Code Playgroud)