在C#中的接口成员访问修饰符 [英] Access modifiers on interface members in C#

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

问题描述

我从以下属性得到一个编译错误。结果
中的错误是:




修改器公众是无效的此项目




 公共System.Collections.Specialized.StringDictionary IWorkItemControl.Properties 
{
获得{返回性能; }
集合{=属性值; }
}



但如果我删除IWorkItemControl它编译罚款。



为什么会出现这个错误,是有/在签名不具有接口名称?


解决之差方案

显式接口实现没有让你指定的任何访问修饰符。当你实现一个接口成员明确(由会员名前指定接口名称),您可以访问该成员的仅在使用该接口。基本上,如果你这样做:

  System.Collections.Specialized.StringDictionary IWorkItemControl.Properties 
{
获得{返回属性; }
集合{=属性值; }
}

您不能做的:

  MyClass的X =新MyClass的(); 
VAR测试= x.Properties; //编译失败
//你应该这样做:
VAR测试=((IWorkItemControl)X)的.properties; //通过接口
访问

有几个用例的EII。例如,你要提供一个关闭方法为您的类释放获取的资源,但你还是要落实的IDisposable 。你可以这样做:

 类测试:IDisposable的{
公共无效关闭(){
//释放的资源
}
无效IDisposable.Dispose(){
关闭();
}
}



这样,类的消费者只能拨打关闭直接(他们甚至不会看到的Dispose 在智能感知列表),但你仍然可以使用测试类的地方的的IDisposable 预期(例如,在使用语句)。



另一个用例EII为两个接口提供了一个同名的接口成员的不同实现:

 接口卓智{
布尔属性{搞定; }
}

接口ITwo的{
string属性{获得; }
}

类测试:卓智,ITwo的{
布尔IOne.Property {...}
串ITwo.Property {...}
}

正如你所见,没有EII它的甚至无法实施这个例子中的一个类两个接口(如属性的返回类型只是不同)。在其他情况下,你可能想故意提供通过不同的接口的类的个人意见不同的行为。


I am getting a compile error from the following property.
The error is:

"The modifier 'public' is not valid for this item"

public System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
    get { return properties; }
    set { properties = value; }
}

but if I remove the IWorkItemControl it compiles fine.

Why am I getting this error and what is the difference of having / not having the interface name in the signature?

解决方案

Explicit interface implementation does not let you specify any access modifiers. When you implement an interface member explicitly (by specifying the interface name before the member name), you can access that member only using that interface. Basically, if you do:

System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
    get { return properties; }
    set { properties = value; }
}

You can't do:

MyClass x = new MyClass();
var test = x.Properties; // fails to compile
// You should do:
var test = ((IWorkItemControl)x).Properties; // accessible through the interface

There are several use cases for EII. For example, you want to provide a Close method for your class to free up acquired resources but you still want to implement IDisposable. You could do:

class Test : IDisposable {
    public void Close() {
        // Frees up resources
    }
    void IDisposable.Dispose() {
        Close();
    }
}

This way, the consumers of class can only call Close directly (and they won't even see Dispose in Intellisense list) but you can still use the Test class wherever an IDisposable is expected (e.g. in a using statement).

Another use case for EII is providing different implementations of an identically named interface member for two interfaces:

interface IOne {
   bool Property { get; }
}

interface ITwo {
   string Property { get; }
}

class Test : IOne, ITwo {
   bool IOne.Property { ... }
   string ITwo.Property { ... }
}

As you see, without EII it's not even possible to implement both interfaces of this example in a single class (as the properties differ just in return type). In other cases, you might want to intentionally provide different behavior for individual views of a class through different interfaces.

这篇关于在C#中的接口成员访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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