简体   繁体   English

如何调用已实现的接口函数

[英]How do I call an implemented interface function

I have a "Page" that implement my own "Permission" interface. 我有一个实现我自己的“权限”界面的“页面”。

public interface PagePermissions{
      Dictionary<string, Permission> readPermissions();
}


public partial class myWebPage: System.Web.UI.Page, PagePermissions
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     Dictionary<string, Permission> PagePermissions.readPermissions()
    {
        Dictionary<string, Permission> results = new Dictionary<string, Permission>();
        return results;
    }
}

In my master page, I get a reference to the current Page object. 在我的母版页中,获得对当前Page对象的引用。

  Page myPage = HttpContext.Current.Handler as Page;

But I can't call my function ReadPermission because it's not recognizing it: 但是我无法调用我的函数ReadPermission,因为它无法识别它:

  myPage.readPermissions();

How do I call my implemented function? 如何调用我的已实现函数?

You need to cast myPage to PagePermissions . 您需要将myPage PagePermissionsPagePermissions

PagePermissions myPage = HttpContext.Current.Handler as PagePermissions;
myPage.readPermissions();

尝试将myPage投射到您的界面。

PagePermissions myPagePermissions = (PagePermissions)myPage;

Why are you using explicit implementation of the interface function? 为什么要使用接口函数的显式实现? Think about changing the code line 考虑更改代码行

Dictionary<string, Permission> PagePermissions.readPermissions()
{

to

public Dictionary<string, Permission> readPermissions()
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何找到实现接口的类 - How do I find the class that implemented an interface 如何使用反射确定已实现接口的通用类型? - How do I determine the generic type of an implemented interface using reflection? 如何调用类中实现的接口的方法? - how to call methods of a interface implemented in a class? 如何强制调用接口实现的方法? - How to forcibly call interface implemented method? 如何从类中调用方法,但如何从任何接口实现此方法? - How can i call method from class but this method implemented from any interface? 如何定义必须由类实例实现的功能的签名? - How do I define the signature of a function that must be implemented by instances of a class? 我想使用由另一个类实现的接口来调用函数,而无需在c#中创建该类的实例 - I want to use an interface that is being implemented by another class to call a function without making an instance of said class in c# 我如何知道何时在忽略继承的类型中直接实现接口? - How do I know when an interface is directly implemented in a type ignoring inherited ones? 如何正确使用显式实现的接口属性和wpf可见性? - How do I use an explicitly implemented interface property and wpf visiblity together properly? 如何使用Type.InvokeMember来调用显式实现的接口方法? - How to use Type.InvokeMember to call an explicitly implemented interface method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM