2012-05-31 82 views
3

什麼不對這個?:爲什麼我得到「不一致的可訪問性:屬性類型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

+2

現在如果你繼承EFNLBaseRepository在另一個組件(完全合法的)你將如何從裝配訪問自己的屬性,它僅供內部使用抽象類的組件?使mamber內部和私人,因爲你只能在抽象類中使用它,而不是暴露出來。 –

+0

事情是我想派生類(只從相同的程序集)有權訪問dbContext成員...我該怎麼做? –

回答

2

protected internal give all subc即使當子類在DLL之外時,也可以訪問該屬性。這與屬性internal的類型不一致,因爲它需要外部的子類才能訪問內部類型。

考慮這個例子:我繼承從EFNLBaseRepository您的DLL

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

好吧,我得到了錯誤......那麼我的問題是:我該如何做dbContext成員只能從相同的程序集派生類訪問? –

+0

@CristianGrisolia沒有辦法像這樣設置它,同時保持「EFNLBaseRepository」類的公開。如果您需要在包中的類之間共享實現,請創建一個公共接口'IEFNLBaseRepository',使'EFNLBaseRepository'內部,make和'dbContext'受到保護。 – dasblinkenlight

0

外的問題是,另一asembly可以固有EFNLBaseRepository類,並且這種情況下,internal是使它派生類不太容易接近。由於這個衝突編譯器不允許它。

相關問題