错误:set访问的可访问修饰符必须比属性或索引更严格 [英] ERROR: the accessibility modifier of the set accessor must be more restrictive than the property or indexer
问题描述
我有一个有点混乱与属性访问器。
i'm having a bit of confusion with property accessors.
我想有它的set访问内部属性只访问派生类。
I'd like to have an internal property with its set accessor only accessible to derived classes.
像这样
internal [internalClass] MyProperty
{
get {return _prop;}
protected set {_prop = value;}
}
当我这样做的编译器抱怨。
when i do this the compiler complains.
MSDN,讨论这个特定错误时,建议更改设置
访问修饰符私人
MSDN, when discussing this particular error suggests changing the set
access modifier to private
这是不是我想要这个去。
which is not where i want this to go.
它看起来像受保护的内部
要使用这条修改一个不错的选择给出了同样的错误。
it looks like Protected Internal
should be an option here however using this modifier gives the same error
我有一种感觉,我缺少访问修饰符的一些基本的了解。
I have a feeling i'm missing some basic understanding of access modifiers.
感谢
推荐答案
无论是保护
也不受保护的内部
更比内部限制性
。双方将让派生类型从不同的装配访问setter方法而不是吸气。 受保护的内部
给出了保护
和内部访问的联盟的
,不是的路口的。 (这里有代表在CLR路口的访问级别,但它不是由C#曝光。)
Neither protected
nor protected internal
is more restrictive than internal
. Both would let derived types from a different assembly access the setter but not the getter. protected internal
gives access to the union of protected
and internal
, not the intersection. (There is an access level representing the intersection in the CLR, but it's not exposed by C#.)
您可能是最好还是使用私人setter和一个受保护的 SetMyProperty
方法,它只是调用私有制定者,如果符合你想要达到的目标。
You might be best off using a private setter and a protected SetMyProperty
method which just calls the private setter, if that matches what you want to achieve.
这篇关于错误:set访问的可访问修饰符必须比属性或索引更严格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!