Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有显式转换的情况下在内部调用显式接口实现方法?_C#_.net - Fatal编程技术网

C# 如何在没有显式转换的情况下在内部调用显式接口实现方法?

C# 如何在没有显式转换的情况下在内部调用显式接口实现方法?,c#,.net,C#,.net,如果我有 public class AImplementation:IAInterface { void IAInterface.AInterfaceMethod() { } void AnotherMethod() { ((IAInterface)this).AInterfaceMethod(); } } 如何从AnotherMethod()调用AInterfaceMethod(),而无需显式强制转换?您不能从方法签名中删除“IAInterf

如果我有

public class AImplementation:IAInterface
{
   void IAInterface.AInterfaceMethod()
   {
   }

   void AnotherMethod()
   {
      ((IAInterface)this).AInterfaceMethod();
   }
}
如何从
AnotherMethod()
调用
AInterfaceMethod()
,而无需显式强制转换?

您不能从方法签名中删除“IAInterface.”吗

public class AImplementation : IAInterface
{
   public void AInterfaceMethod()
   {
   }

   void AnotherMethod()
   {
      this.AInterfaceMethod();
   }
}

尝试了这个,它的工作

public class AImplementation : IAInterface
{
    IAInterface IAInterface;

    public AImplementation() {
        IAInterface = (IAInterface)this;
    }

    void IAInterface.AInterfaceMethod()
    {
    }

    void AnotherMethod()
    {
       IAInterface.AInterfaceMethod();
    }
}

您不能,但如果您必须经常这样做,您可以定义一个方便助手:

private IAInterface,它{get{返回(IAInterface)this;}}


每当您想要调用显式实现的接口方法时,您可以使用
that.method()
而不是
((IAInterface)this.method()

您可以引入助手私有属性:

private IAInterface IAInterface => this;

void IAInterface.AInterfaceMethod()
{
}

void AnotherMethod()
{
   IAInterface.AInterfaceMethod();
}

很多答案都说你不能。他们是不正确的。有很多方法可以在不使用cast操作符的情况下执行此操作

void AnotherMethod()   
{      
    (this as IAInterface).AInterfaceMethod();  // no cast here
}
技巧1:使用“as”操作符而不是cast操作符

void AnotherMethod()   
{      
    (this as IAInterface).AInterfaceMethod();  // no cast here
}
技巧2:通过局部变量使用隐式转换

void AnotherMethod()   
{      
    IAInterface ia = this;
    ia.AInterfaceMethod();  // no cast here either
}
技巧3:编写一个扩展方法:

static class Extensions
{
    public static void DoIt(this IAInterface ia)
    {
        ia.AInterfaceMethod(); // no cast here!
    }
}
...
void AnotherMethod()   
{      
    this.DoIt();  // no cast here either!
}
技巧4:介绍助手:

private IAInterface AsIA => this;
void AnotherMethod()   
{      
    this.AsIA.IAInterfaceMethod();  // no casts here!
}
还有另一种方式(不是最好的):

还有另一种方法(这是Eric的技术#2的衍生产品,如果接口没有实现,也应该给出编译时错误)


演员阵容有什么问题?当我发现这个语言特征时,我只是皱了皱眉头。但是,在实现某些接口(如ICloneable)时,它非常有用。为什么不反过来呢?将代码从显式接口方法移动到“普通”方法。然后让所有方法(包括显式接口方法)调用该方法。IMHO,@adrianm上面的评论就是最好的答案![当您遇到这种情况时,或者如果您希望需要,这是一个很好的重构。]FWIW在几乎所有情况下,一个“解决方法”是将该方法更改为隐式实现,以便在满足接口约定的同时允许内部访问。缺点是通过声明成员
public
(并从声明中删除显式
IAInterface.
)可以实现这一点,这使得它比您希望的更为广泛。因此,我不建议这样做,只是把它作为一种可能的替代解决方案提出来。这样它就不会是一个显式的实现。值得注意的是,这里不需要显式的强制转换(因为这里有一个从
this
IAInterface
的隐式转换,后者是实现的)。要学究,他没有要求不使用“cast操作符”,他要求“不使用显式强制转换”,我认为,一般来说,操作符
as
将被视为“显式强制转换”(不知道这在语言规范定义方面是否正确,或者这个问题在该上下文中是否毫无意义),cast操作符和as操作符的语义完全不同,因此将它们混为一谈是一个逻辑错误。我知道这已经很老了,但我今天偶然发现了这个问题。我认为“最好”的方式是含蓄的方式,对吗?这为代码提供了最好的编译时保护,对吗?如果我使用强制转换或“as”操作(在这种情况下实际上没有意义,因为对象应该首先实现接口),我就有可能尝试强制转换到一个无效的接口,从而导致运行时错误。如果我试图隐式转换到类未实现的接口,则会出现编译时错误,with更好。@julealgon:您提出了一个令人信服的论点。@zverev.Eugene:如果类
a
的作者希望派生类必须能够调用和/或重写基类实现,那么类
a
的作者应该创建一个受保护的虚拟方法,而不是显式的接口方法。如果类
A
的作者不打算这样做,而类
B
的作者希望类
A
的作者已经这样做了,那么类
B
的作者应该(1)找到一个不同的类来扩展,或者(2)与类
A
的作者协商。继承必须是类的一个设计特性。考虑到编译器如何使其工作,这只是说
((IAInterface)this)
的一种麻烦的方式,还有一个缺点,即如果
this
null
,并且不应该是,它将对类的默认实例调用
AInterfaceMethod
,而不是抛出异常。几乎肯定不需要行为。我认为这比Eric的技巧更好#2-如果您希望在接口中的多个方法中访问其他接口方法引用。您不必显式地将其转换为IAInterface
     IAInterface AsIAInterface
     {
        get { return this; }
     }