2013-07-25 101 views
5

我有一個接口,並在同一個組件定義一個抽象基類:內部接口*少於內部受保護構造函數可訪問?

IFoo.cs:

internal interface IFoo { ... } 

Base.cs:

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

這將生成以下編譯器錯誤:

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

如果我只將基類構造函數作爲內部函數,錯誤就會消失。由於課程是抽象的,也許加入保護到可訪問性並沒有完成任何事情......

不過,我不明白錯誤。 MSDN定義「受保護的內部」作爲

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

內部接口IFoo的比內受保護的構造少訪問是怎樣的?

回答

8

This MSDN page定義的「受保護的內部」作爲(重點從原來的):

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.

所以,換句話說,從目前的集之外的類型,從Base派生將有機會獲得Base(IFoo foo),但他們不會有訪問IFoo,因爲它是內部的。因此錯誤。

+0

非常好,簡潔的解釋。 – Lemonseed