我有一个实现接口的类IPerson
。我想从我的类中调用接口中实现的方法,但出现此错误:CS0103 当前上下文中不存在名称“SayMyName”如何从派生类调用接口中实现的方法?
public interface IPerson
{
string SayMyName()
{
return "MyName";
}
}
public class Person : IPerson
{
public void Method()
{
SayMyName();//ERROR CS0103 The name 'SayMyName' does not exist in the current context
}
}
您需要显式转换才能访问由接口提供的默认实现。
((IPerson)this).SayMyName();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句