Pregunta

I have an interface and an abstract base class defined in the same assembly:

IFoo.cs:

internal interface IFoo { ... }

Base.cs:

public abstract class Base
{
    internal protected Base(IFoo foo) { ... }
}

This generates the following compiler error:

CS0051: Inconsistent accessibility: parameter type 'IFoo' is less
        accessible than method 'Base.Base(IFoo)'

If I make the Base class constructor internal-only, the error goes away. Since the class is abstract, maybe adding protected to the accessibility doesn't accomplish anything...

Still, I don't understand the error. MSDN defines 'protected internal' as

"Access is limited to the current assembly or types derived from the containing class"

How is the internal interface IFoo less accessible than an internal protected constructor?

¿Fue útil?

Solución

This MSDN page defined 'protected internal' as (emphasis from original):

The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

So in other words, types from outside the current assembly that derive from Base would have access to Base(IFoo foo) but they wouldn't have access to IFoo, since it is internal. Thus the error.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top