为什么我得到“不一致的可访问性:属性类型xxx ……”?

这有什么问题?:

public abstract class EFNLBaseRepository:IDisposable { NLSubscriberDBContext _dbContext; protected internal NLSubscriberDBContext dbContext { get {...} } ... } internal class NLSubscriberDBContext : DbContext { ... } 

当然,这两个类都在同一个程序集中。 这是我得到的编译错误:

错误1可访问性不一致:属性类型’NLSubscriber.Core.Service.Repository.EFDAL.NLSubscriberDBContext’的可访问性低于属性’NLSubscriber.Core.Service.Repository.EFDAL.EFNLBaseRepository.dbContext’C:\ Data \ Projects \ Neticon \ TFS \ NLSubscriber – Newsletter \ NLSubscriber-newsletter \ NLSubscriber.Core \ Service \ Repository \ EFDAL \ EFNLBaseRepository.cs 12 50 NLSubscriber.Core

protected internal为所有子类提供对属性的访问,即使子类在DLL之外也是如此。 这与internal属性的类型不一致,因为它需要外部的子类才能访问内部类型。

考虑这个例子:我从你的DLL外部子类化EFNLBaseRepository

 public sealed EFNLSealedRepository : EFNLBaseRepository { public DoSomething() { // Access to dbContext should be allowed, because it is protected; // However, NLSubscriberDBContext should not be accessible. // This is an inconsistency flagged by the C# compiler. NLSubscriberDBContext context = dbContext; } } 

问题是另一个EFNLBaseRepository可以固有EFNLBaseRepository类,而internal使得派生类不太容易访问它。 因为冲突编译器不允许它。

protected internal:访问仅限于从包含类派生的当前程序集类型。

http://msdn.microsoft.com/en-us/library/ba0a1yw2(v=VS.80).aspx