与内部实现C#内部接口 [英] C# internal interface with internal implementation

查看:190
本文介绍了与内部实现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();
}



然后

and then

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;
}



然后我有类:

Then I have the class:

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;
}



为了让事情更奇怪,我创建了另一个名为温度内部类。然后,我曾就落实所以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)

编辑:附加信息

我通过反射运行的代码和我的实现已经编译成明确的实现,即使它们不明确。反射镜显示没有内部关键字的迹象。所有我可以猜测是,这是某种在于,由于某种原因,允许我使它们内部和隐编译毛刺的,它以某种方式解决的,由于作为一个明确的实施。

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.

internal 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天全站免登陆