实现接口C# [英] Implementing interface C#

查看:86
本文介绍了实现接口C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#并不陌生,但是我发现了一个令人费解的行为.

I'm not new to C#, but I have found a behavior that is a little puzzling.

我有一个界面

public interface IApplicationPage
{
    Person ThePerson { get; set;  }
    Application Application { get; set; }
}

我在页面上实现界面

public partial class tripapplication2 : System.Web.UI.Page, IApplicationPage
{
    Person IApplicationPage.ThePerson { get; set; }
    Application IApplicationPage.IApplicationPage.Application { get; set; }
}

但是,当我尝试在页面本身中引用ThePerson时,我需要跳过去.例如.

However, when I attempt to reference ThePerson in the page itself I need to jump through hoops. For example.

1)ThePerson.Birthday

1) ThePerson.Birthday

给出一个错误消息:名称'ThePerson'在当前上下文中不存在."

Gives an error saying "The name 'ThePerson' does not exist in the current context."

2)(((IMissionTripApplicationPage)this).ThePerson.Birthday

2) ((IMissionTripApplicationPage)this).ThePerson.Birthday

这可行,但是看起来很糟糕.

This works, but it looks awful.

是否有更好的方法来引用已实现的属性?

Is there a better way to reference the implemented properties?

推荐答案

好像您在示例中留下了一条线.我相信实施中的ThePerson行应显示为

It looks like you left a line out in your sample. I believe the ThePerson line in the implementation should read

Person IApplicationPage.ThePerson { get; set; }

这种类型的实现称为显式接口实现.这意味着仅当通过接口类型的引用看到对象时,该成员才可见.

This type of implementation is known as an explicit interface implementation. This means the member will only be viewable when the object is seen through a reference of the interface type.

如果您希望通过具体的类型引用查看该成员,请将其公开并删除显式实现

If you want the member to be viewable through a concrete type reference, make it public and remove the explicit implementation

public Person ThePerson { get; set; }

这篇关于实现接口C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆