简体   繁体   English

C#内部访问修饰符

[英]C# internal access modifiers

I want to seal of classes in a namespace. 我想在命名空间中密封类。 I was looking at the "internal" access modifier, but this only seems to seal of classes in an assembly. 我正在查看“内部”访问修饰符,但这似乎只是在程序集中的类的封印。 Can I seal of classes in a namespace? 我可以在命名空间中封装类吗?

Or do I have to move stuff into an seperate assembly? 或者我必须将东西移动到单独的组件中? But then I will have the problem of visual studio refusing circular assembly references. 但后来我会遇到视觉工作室拒绝循环装配参考的问题。

No, there is no namespace-specific modifier. 不,没有特定于命名空间的修饰符。 One option would be to use inheritance and a "protected" modifier, but having an internal constructor on the base-class so that external code can't subclass it. 一种选择是使用继承和“受保护”修饰符,但在基类上有一个内部构造函数,以便外部代码不能对其进行子类化。 That might help. 这可能有所帮助。

It is not possible with C#. 使用C#是不可能的。

Namespace-level members can only be either public or internal 命名空间级别的成员只能是public成员或internal

You can however, use nested class in C# 但是,您可以在C#中使用嵌套类

namespace A {
    public class B {

        protected class C { }
    }

    public class D {

        void E() {
            var F = new A.B();    // ok!
            var G = new A.B.C();  // error!
        }
    }
}

您仍然可以使用internal关键字,并通过AssemblyInfo.cs文件中的InternalsVisibleTo属性将内部类公开给另一个程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM