C# internal interface with internal implementation

前端 未结 6 693
南笙
南笙 2020-12-15 15:03

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 als

相关标签:
6条回答
  • 2020-12-15 15:36

    If your intention is to hide a certain implementation from outside, you can implement it explicitly like this:

    internal class LDialogService : ILDialogService, ILDialogInternalService
    {
    
        public async Task<TValue> ShowAsync<TValue>(ILDialogFragment fragment)
        {
            throw new NotImplementedException();
        }
    
        void ILDialogInternalService.SetComponent(LDialog component)
        {
            throw new NotImplementedException();
        }
    }
    

    In the above code, I want to expose ShowAsync method to the outside but keep SetComponent inside. Since ILDialogInternalService is internal, no one can call it from outside except through Reflection.

    0 讨论(0)
  • 2020-12-15 15:38

    Probably that your "Temp" class is public and IScanner is internal. This is the reason why you get this error. I consider this very annoying since your are forced to implement it explicitly you cannot specify them as abstract or virtual. For the virtual stuff, I was forced to do an implicit internal virtual implementation of the same API and then call the implicit version from the explicit one. Ugly.

    0 讨论(0)
  • 2020-12-15 15:43

    It's OK to have an internal modifier in an Interface declaration however you CAN'T have ANY modifiers INSIDE the interface, in other words, you can't have any modifier for the interface Members. It's simple as that!

    Example:

    internal interface IA
    {
        void X(); //OK. It will work
    }
    
    
    internal class CA : IA
    {
        **internal** void X() // internal modifier is NOT allowed on any Interface members. It doesn't compile. If it works in your project it's because either you DON'T have the void X() method in the Interface or your are inheriting from a wrong interface maybe accidentally 
        {
            ...
        }
    }
    

    An interface declaration may declare zero or more members. The members of an interface must be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can an interface contain static members of any kind. All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.

    Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces

    0 讨论(0)
  • 2020-12-15 15:46

    To all my knowledge you cannot implement interface methods internal. As you stated you can implement them explicitly but then someone can still do ((IScanner)myScanner).SetHardware(hw)

    Are you 100% sure your MyScanner implementation does not do something like this:

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

    or this:

    internal partial class MyScanner : IScanner
    {
        internal void SetHardware(Hardware hardware)
        {
            ...
        }
    }
    
    internal partial class MyScanner
    {
        void IScanner.SetHardware(Hardware hardware)
        {
            this.SetHardware(hardware);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 15:56

    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 */ }
    

    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!

    0 讨论(0)
  • 2020-12-15 16:03

    I know it has been a while since this question was asked, but maybe I can shed some light on it. According to the C# language specification found here the behavior you described should not be possible. Because under 20.4.2 Interface mapping it is said that the implementation is either explicit or mapped to a public non-static member. So either you have some other scenario than the one you are describing here, or you found a bug in your compiler :).

    0 讨论(0)
提交回复
热议问题