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

本文关键字:类型 属性 xxx quot 访问 不一致 为什么 | 更新日期: 2023-09-27 18:35:48

这有什么问题?

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

internal class NLSubscriberDBContext : DbContext
{
  ...
}

当然,这两个类位于同一个程序集上。这是我得到的编译错误:

错误 1 可访问性不一致:属性类型 'NLSubscriber.Core.Service.Repository.EFDAL.NLSubscriberDBContext' is 比物业更难到达 '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

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

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

问题是另一个 asembly 可以固有EFNLBaseRepository类,在这种情况下,internal 使派生类更难访问。由于该冲突,编译器不允许它。

受保护的内部: 访问仅限于当前程序集从包含类派生的类型。

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