加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我们可以使用接口变量调用其接口中未定义的实现类中的方法

发布时间:2020-12-15 05:39:44 所属栏目:百科 来源:网络整理
导读:是否可以在接口的实现类中调用一个方法,该接口未使用接口变量在接口中定义,如下所示: interface ILookup { public void someInterfaceMethod1(); public void someInterfaceMethod2();} ……和实施类: public class extendedLookUpImplementor: ILookup{ /
是否可以在接口的实现类中调用一个方法,该接口未使用接口变量在接口中定义,如下所示:
interface ILookup {
            public void someInterfaceMethod1();
            public void someInterfaceMethod2();
}

……和实施类:

public class extendedLookUpImplementor: ILookup
{
          //constructor
          LookUpImplementor(){
          }

          public void someInterfaceMethod1(){
           // Implementation Code here.
          }

          public void someInterfaceMethod2(){
           // Implementation Code here.
          }

          public void ExtendedMethod(){
           // Implementation Code here.
          }

}

在客户端代码中:

ILookup lookupVar = new LookUpImplementor();
lookupVar -> someInterfaceMethod1(); // i know it will work.
lookupVar -> someInterfaceMethod2(); // i know it will work.

我的问题是,我可以使用lookupVar调用ExtendedMethod,如下所示:

lookupVar -> ExtendedMethod(); // Note again that ExtendedMethod() is not defined in Ilookup interface/contract.

解决方法

只能通过将lookupVar强制转换为extendedLookUpImplementor,或者通过反射我认为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读