本文介绍了为什么我越来越"可访问性不一致:属性类型XXX ..."?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是错的:
公共抽象类EFNLBaseRepository:IDisposable的
{
NLSubscriberDBContext _dbContext;
受保护的内部NLSubscriberDBContext的DbContext
{
得到
{...}
}
:
}
内部类NLSubscriberDBContext:的DbContext
{
:
}
当然,这两个类都在相同的生产。
这是我得到的编译错误:
解决方案
受保护的内部
给出了所有子类访问属性,子类甚至当是DLL之外。这是与属性为内部
,因为它需要来自外部的子类,以访问该内部型
的类型不一致
考虑这个例子:我继承 EFNLBaseRepository
从您的DLL外
公共密封EFNLSealedRepository:EFNLBaseRepository {
公共DoSomething的(){
//访问的DbContext应该被允许的,因为它是受保护的;
//然而,NLSubscriberDBContext不应该访问。
//这是C#编译器标记不一致。
NLSubscriberDBContext背景=的DbContext;
}
}
What is wrong with this?:
public abstract class EFNLBaseRepository:IDisposable
{
NLSubscriberDBContext _dbContext;
protected internal NLSubscriberDBContext dbContext
{
get
{...}
}
...
}
internal class NLSubscriberDBContext : DbContext
{
...
}
Of course, both classes are on the same assembly.This is the compilation error I'm getting:
解决方案
protected internal
gives all subclasses access to the property, even when the subclass is outside the DLL. This is inconsistent with the type of the property being internal
, because it would require a subclass from the outside to have access to the internal type.
Consider this example: I subclass EFNLBaseRepository
from outside your 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;
}
}
这篇关于为什么我越来越"可访问性不一致:属性类型XXX ..."?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!