![](/img/trans.png)
[英]Why i'm getting Inconsistent accessibility: field type … is less accessible than field ? And how to fix it?
[英]Why I'm getting “Inconsistent accessibility: property type 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.