Type.IsPublic 属性

定义

获取一个值,该值指示 Type 是否声明为公共类型。

C#
public bool IsPublic { get; }

属性值

Boolean

如果 true 声明为公共类型且不是嵌套类型,则为 Type;否则为 false

实现

示例

下面的示例创建一个实例 MyTestClass ,检查 IsPublic 属性,并显示结果。

C#
using System;

public class TestClass
{
}

public class Example
{
   public static void Main()
   {
      TestClass testClassInstance = new TestClass();
      // Get the type of myTestClassInstance.
      Type   testType = testClassInstance.GetType();
      // Get the IsPublic property of testClassInstance.
      bool isPublic = testType.IsPublic;
      Console.WriteLine("Is {0} public? {1}", testType.FullName, isPublic);
   }
}
// The example displays the following output:
//        Is TestClass public? True

对于嵌套类,忽略和的结果,并 IsPublic IsNotPublic 只注意和的结果 IsNestedPublic IsNestedPrivate

注解

不要将与嵌套类型一起使用;改为使用 IsNestedPublic

如果当前 Type 表示泛型类型的类型参数,则此属性返回 true

TypeAttributes.VisibilityMask 选择可见性特性。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

另请参阅