Type.IsNotPublic 屬性

定義

取得值,指出 Type 是否未宣告為公用。Gets a value indicating whether the Type is not declared public.

public:
 property bool IsNotPublic { bool get(); };
public bool IsNotPublic { get; }
member this.IsNotPublic : bool
Public ReadOnly Property IsNotPublic As Boolean

屬性值

如果 true 尚未宣告為公用而且不是巢狀類型,則為 Type,否則為 falsetrue if the Type is not declared public and is not a nested type; otherwise, false.

實作

範例

這個範例會使用 IsNotPublic 屬性來取得型別的可見度。This example uses the IsNotPublic property to get the visibility of the type.

using namespace System;
using namespace System::IO;
using namespace System::Reflection;

int main()
{
   //Get the Type and MemberInfo.
   Type^ t = Type::GetType("System.IO.File");
   array<MemberInfo^>^ members = t->GetMembers();
   
   //Get and display the DeclaringType method.
   Console::WriteLine("There are {0} members in {1}.",
                      members->Length, t->FullName );
   Console::WriteLine("Is {0} non-public? {1}",
                      t->FullName, t->IsNotPublic );
}
// The example displays the following output:
//       There are 60 members in System.IO.File.
//       Is System.IO.File non-public? False
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
Imports System.IO
Imports System.Reflection

Module Example
    Public Sub Main()
        'Get the Type and MemberInfo.
        Dim t As Type = Type.GetType("System.IO.File")
        Dim members As MemberInfo() = t.GetMembers()
        'Get and display the DeclaringType method.
        Console.WriteLine("There are {0} members in {1}.",
                          members.Length, t.FullName)
        Console.WriteLine("Is {0} non-public? {1}",
                          t.FullName, t.IsNotPublic)
    End Sub
End Module
' The example displays output like the following:
'       There are 60 members in System.IO.File.
'       Is System.IO.File non-public? False

下列程式碼範例示範為何無法使用嵌套類別的 IsPublicIsNotPublicThe following code example demonstrates why you cannot use IsPublic and IsNotPublic for nested classes.

public ref class A
{
public:
   ref class B{};


private:
   ref class C{};


};

public class A 
{
    public class B { }
    private class C { }
}
Public Class A
    Public Class B
    End Class
    Private Class C
    End Class
End Class

針對嵌套類別,請忽略 IsPublicIsNotPublic 的結果,並只注意 IsNestedPublicIsNestedPrivate的結果。For nested classes, ignore the results of IsPublic and IsNotPublic and pay attention only to the results of IsNestedPublic and IsNestedPrivate. 此程式碼片段的反映輸出如下所示:The reflection output for this code fragment would be as follows:

類別Class IsNotPublicIsNotPublic IsPublicIsPublic IsNestedPublicIsNestedPublic IsNestedPrivateIsNestedPrivate
AA FALSEFALSE TRUETRUE FALSEFALSE FALSEFALSE
BB FALSEFALSE FALSEFALSE TRUETRUE FALSEFALSE
CC FALSEFALSE FALSEFALSE FALSEFALSE TRUETRUE

備註

請勿將這個屬性與巢狀型別搭配使用;請改用 IsNestedPublic 屬性。Do not use this property with nested types; use the IsNestedPublic property instead.

如果目前的 Type 表示泛型型別的類型參數,則這個屬性會傳回 falseIf the current Type represents a type parameter of a generic type, this property returns false.

適用於

另請參閱