在.NET中保护类 [英] Protected Classes in .NET

查看:109
本文介绍了在.NET中保护类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否一类被保护in.NET?
为什么是/不是这可能吗?

Can a class be protected in.NET?
Why is / isn't this possible?

推荐答案

是的,你不能让他们的顶级类,它们必须是内部类

Yes, you just cannot make them top level classes, they must be inner classes

public class Outer
{
    protected class Foo
    {
    }
}

这是好的,它意味着允许查看富的唯一类外的子类

This is fine, it means that the only classes allowed to see Foo are sub classes of Outer

class X 
{
    // 'Outer.Foo' is inaccessible due to its protection level
    private void Flibble(Outer.Foo foo)
    {
    }
}

class X : Outer
{
    // fine
    private void Flibble(Outer.Foo foo)
    {
    }
}

请注意,你不能声明任何外部类为私有,受保护(或受保护的内部)在c#因为对于外层类访问修饰符定义了相对于其他组件自己的知名度。具体可见组件内唯一的(或通过InternalsVisibleTo朋友)或集之外。

Note that you cannot declare any outer class as private, protected (or protected internal) in c# since the access modifier for outer level classes defines their visibility in relation to other assemblies. Specifically visible within the assembly only (or to friends via InternalsVisibleTo) or outside the assembly.

因此​​而公共/内部标识符这里用于一致性真正IL的状态简直是公开或非公开(为的Reflection.Emit标志显示

Thus whilst the public/internal identifiers are used here for consistency really the state in IL is simply 'Public' or 'NonPublic' (as the Reflection.Emit flags show)

这篇关于在.NET中保护类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆