【发布时间】:2014-07-27 17:16:53
【问题描述】:
list of access modifiers 为我们提供 public、private、protected、internal 和 protected internal。在编写嵌套类时,我发现自己想要一个魔术修饰符组合(不太可能)或编码模式,它给我private 或protected,但包含类也可以访问。有这种事吗?
示例 - 类或其容器类中的公共属性仅代码可以设置:
public class ContainingClass {
public class NestedClass {
public int Foo { get; magic_goes_here set; }
}
void SomeMethod() {
NestedClass allow = new NestedClass();
allow.Foo = 42; // <== Allow it here, in the containing class
}
}
public class Unrelated {
void OtherMethod() {
NestedClass disallow = new ContainingClass.NestedClass();
disallow.Foo = 42; // <== Don't allow it here, in another class
}
}
有没有办法做到这一点?同样,除非我在上面链接的页面上错过了一个神奇的组合,否则可能不是字面上的访问修饰符,但如果不是现实,我可以使用一些模式来使其生效?
【问题讨论】:
标签: c# access-modifiers