Type.IsNotPublic 屬性

定義

取得值,指出 Type 是否未宣告為公用。

C#
public bool IsNotPublic { get; }

屬性值

如果 Type 尚未宣告為公用而且不是巢狀型別,則為 true,否則為 false

實作

範例

這個範例會 IsNotPublic 使用 屬性來取得型別的可見度。

C#
using System;
using System.IO;
using System.Reflection;

class Example
{
    public static void Main()
    {
        // Get the Type and MemberInfo.
        Type t = Type.GetType("System.IO.File");
        MemberInfo[] members = t.GetMembers();
        // Get and display the DeclaringType method.
        Console.WriteLine("\nThere are {0} members in {1}.",
                          members.Length, t.FullName);
        Console.WriteLine("Is {0} non-public? {1}",
                          t.FullName, t.IsNotPublic);
    }
}
// The example displays output like the following:
//       There are 60 members in System.IO.File.
//       Is System.IO.File non-public? False

下列程式碼範例示範為何您無法針對巢狀類別使用 IsPublicIsNotPublic

C#
public class A
{
    public class B { }
    private class C { }
}

對於巢狀類別,請忽略 和 IsNotPublic 的結果 IsPublic ,並只注意 和 IsNestedPrivate 的結果 IsNestedPublic 。 此程式碼片段的反映輸出如下所示:

類別 IsNotPublic IsPublic IsNestedPublic IsNestedPrivate
A FALSE true FALSE FALSE
B FALSE FALSE true false
C FALSE FALSE FALSE true

備註

請勿將此屬性與巢狀類型搭配使用;請改用 IsNestedPublic 屬性。

如果目前的 Type 表示泛型型別的型別參數,這個屬性會傳 false 回 。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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, 4.8.1
.NET Standard 2.0, 2.1

另請參閱