带有内部实现的 C# 内部接口 [英] C# internal interface with internal implementation

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

问题描述

我发现了一些我不太理解的东西.

I've struck upon something I don't really understand.

我有一个项目,其中有一个内部接口.实现该接口的类也是内部的.在接口的实现中,我使我实现的所有成员都成为内部成员.我没有做明确的实现.

I have a project, where I have an interface that is internal. The class that implements that interface is also internal. In the implementation of the interface, I make all the members that I implement, internal. I did not do an explicit implementation.

我有两个接口和两个实现这些接口的类,这些接口可以正常工作.

I have two interfaces and two classes that implement those interfaces where this works fine.

它看起来像这样:

internal interface IA
{
    void X();
}

然后

internal class CA : IA
{
    internal void X()
    {
        ...
    }
}

这适用于上述两个类.但是当我尝试用另一个接口和类来做它时,它不起作用.事实上,对于上面的例子,我得到了错误:

This works fine for the two aforementioned classes. But when I try to do it with another interface and class, it doesn't work. In fact, for the example above, I get the error:

'WindowsFormsApplication1.CA' 没有实现接口成员 'WindowsFormsApplication1.IA.X()'.WindowsFormsApplication1.CA.X()"无法实现接口成员,因为它不是公共的.

'WindowsFormsApplication1.CA' does not implement interface member 'WindowsFormsApplication1.IA.X()'. 'WindowsFormsApplication1.CA.X()' cannot implement an interface member because it is not public.

我意识到我可以将方法公开或进行显式实现(并省略内部和公共修饰符),但我只是很困惑为什么它可以与它使用的两个类一起工作,但我似乎是无法在其他任何地方复制它.

I realize I can make the methods public or do an explicit implementation (and omit the internal and public modifiers), but I'm simply confused as to why it works with the two classes it works with and yet I seem to be unable to replicate it anywhere else.

稍微修改一下代码(因为它是机密的),这是在我的项目中实际有效的代码之一.

Butchering the code a bit (because it's confidential), this is one of the ones that actually works in my project.

internal interface IScanner
{
    void SetHardware(Hardware hardware);
    void Start();
    void PauseScan();
    void ResumeScan();
    void Stop();
    bool InScan { get; }
    event ScanCompleteHandler ScanComplete;
}

然后我有课:

internal class MyScanner : IScanner
{
    internal void SetHardware(Hardware hardware)
    {
       ...
    }

    internal void Start()
    {
        ...
    }

    internal void Stop()
    {
        ...
    }

    internal void PauseScan()
    {
        ...
    }

    internal void ResumeScan()
    {
        ...
    }

    internal bool InScan
    {
        get
        {
            ...
        }
    }

    internal event ScanCompleteHandler ScanComplete;
}

为了让事情变得更奇怪,我创建了另一个名为 Temp 的内部类.然后我让它实现了 IScanner 接口,我将实现从 MyScanner 复制并粘贴到它上面,它不会编译,给我一个错误:无法实现接口成员,因为它不是公共的."

To make things even stranger, I created another internal class called Temp. I then had it implement the IScanner interface and I copied and pasted the implementation from MyScanner over to it and it won't compile, giving me the error that: "cannot implement an interface member because it is not public."

谁能解释这种不一致的地方?

Can anyone explain this inconsistency?

谢谢

(更新以修正拼写错误并澄清一些文本)

(Updated to fix a typo and clarify some text)

附加信息

我通过反射器运行代码并且我的实现被编译为显式实现,即使它们不是显式的.Reflector 没有显示内部关键字的迹象.我所能猜测的是,这是编译器中的某种故障,出于某种原因,它允许我将它们设为内部和隐式,并以某种方式将其解决为显式实现.

I ran the code through reflector and my implementations have been compiled as explicit implementations, even though they aren't explicit. Reflector shows no signs of the internal keywords. All I can guess is that this is some sort of glitch in the compiler that, for some reason, allowed me to make them internal and implicit and that it somehow resolved that as being an explicit implementation.

我已经多次查看代码.我找不到任何其他解释.

I've looked over the code a number of times. I can't find any other explanation for it.

推荐答案

如果您隐式地实现一个接口,我认为该成员必须声明为 public.在您的示例中, CA 尝试隐式实现 X() 方法,但未声明为公共方法.如果您想将 X() 保持为内部,那么您应该使用显式接口实现.

If you are implicitly implementing an interface I believe that the member must be declared public. In your example, CA attempts to implicitly implement the X() method but isn't declared public. If you want to keep X() as internal then you should use explicit interface implementation.

void IA.X() { /* stuff */ }

但是,我还要补充一点,将 X() 方法设为 public 无论如何都不会造成任何伤害,因为该类是内部类,因此该成员已经受到该访问修饰符的限制...也就是说,它已经有效地在内部......所以你不妨把它公之于众!

However, I'll also add that making the X() method public wouldn't do any harm anyway as the class is internal so that member is already restricted by that access modifier... That is, it's already effectively internal... So you might as well just make it public!

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

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