简体   繁体   English

如何实现内部接口的成员

[英]How do I implement members of internal interfaces

I have been refactoring the codebase of the project that I am currently on so that classes/interfaces which are not useful beyond the confines of the assembly should be declared as internal (rather than public). 我一直在重构我目前正在进行的项目的代码库,因此除了程序集的范围之外没有用的类/接口应该被声明为内部(而不是公共)。 But I've run into a problem with the following code: 但是我遇到了以下代码的问题:

internal interface IFirstInterface
{
    ...
}

internal interface ISecondInterface
{
    IFirstInterface First{ get; }
    ...
}

public class Implementer : ISecondInterface
{
    public IFirstInterface First {get; private set;}
    ...
}

My questions: 我的问题:

  1. Why do members of internal interfaces have to be publicly implemented? 为什么内部接口的成员必须公开实施? If you implement the interface on an internal class, shouldn't the implemented members be internal? 如果在内部类上实现接口,那么实现的成员不应该是内部的吗? This is not a big issue since the interface members won't be publicly accessible anyway, given the class is internal. 这不是一个大问题,因为接口成员无论如何都不会公开访问,因为类是内部的。 It just seems counter intuitive. 这似乎与直觉相反。

  2. The main problem is with the scenario above since I cannot have a public getter for IFirstInterface since it is purportedly an internal interface ie I get the following error from the compiler: 主要问题是上面的场景,因为我不能有一个IFirstInterface的公共getter,因为它据称是一个内部接口,即我从编译器得到以下错误:

Inconsistent accessibility: property type 'IFirstInterface' is less accessible than property 'Implementer.First' 可访问性不一致:属性类型“IFirstInterface”比属性“Implementer.First”更难访问

Is there any way around this? 有没有办法解决?

Note : I realise that there is probably little value in this refactoring exercise but I thought it would be a good way for me to understand more deeply the implications of the internal modifier. 注意 :我意识到这个重构练习可能没什么价值,但我认为这将是一个很好的方式让我更深入地了解内部修饰符的含义。

Just to note - the code you've actually provided does compile, because Implementer is an internal class. 需要注意的是 - 您实际提供的代码编译的,因为Implementer是一个内部类。 The problem comes when Implementer is public. Implementer公开时,问题就出现了。

The way round this is to use explicit interface implementation: 解决这个问题的方法是使用显式接口实现:

public class Implementer : ISecondInferface
{
    private IFirstInterface first;
    IFirstInterface ISecondInterface.First { get { return first; } }
}

You can't have the setter in there, because you're explicitly implementing the interface which doesn't define the setter. 你不能在那里有setter,因为你明确地实现了没有定义setter的接口。 You could do this as an alternative: 可以这样做:

public class Implementer : ISecondInterface
{
    internal IFirstInterface First { get; private set; }
    IFirstInterface ISecondInterface.First { get { return First; } }
}

It's unfortunate that internal interfaces have public members - it does complicate things like this. 不幸的是,内部接口有公共成员 - 它确实使这样的事情变得复杂。 It would be strange for a public interface to have an internal member (what would it be internal to - the implementer or the declarer?) but for internal interfaces it makes a lot more sense. 公共接口有一个内部成员(实现者或声明者的内部成员是什么?)会很奇怪,但对于内部接口来说,它更有意义。

Why do members of internal interfaces have to be publicly implemented? 为什么内部接口的成员必须公开实施?

When you define an interface, you do not define access level for the members, since all interface members are public . 定义接口时,不要为成员定义访问级别,因为所有接口成员都是public Even if the interface as such is internal , the members are still considered public . 即使这样的界面是internal ,成员仍然被认为是public When you make an implicit implementation of such a member the signature must match, so it needs to be public . 当您对这样的成员进行隐式实现时,签名必须匹配,因此它需要是public

Regarding exposing the getter, I would suggest making an explicit implementation of the interface instead, and creating an internal property to expose the value: 关于暴露getter,我建议改为明确实现接口,并创建一个internal属性来公开值:

internal IFirstInterface First { get; private set; }

IFirstInterface ISecondInterface.First
{
    get { return this.First; }
}

I know this post is a few years old but i think it's worth noting that you can implement an internal interface on a public class, see the following links: 我知道这篇文章有几年了,但我认为值得注意的是你可以在公共类上实现内部接口,请参阅以下链接:

http://forums.create.msdn.com/forums/p/29808/167820.aspx http://forums.create.msdn.com/forums/p/29808/167820.aspx
http://msdn.microsoft.com/en-us/library/aa664591%28VS.71%29.aspx http://msdn.microsoft.com/en-us/library/aa664591%28VS.71%29.aspx

An example from the first link: 第一个链接的示例:


internal interface ISecretInterface
{
    string Property1 { get; }
}

public class PublicClass : ISecretInterface
{
    // class property
    public string Property1
    {
        get { return "Foo"; }
    }

    // interface property
    string ISecretInterface.Property1
    {
        get { return "Secret"; }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM