Type.IsNotPublic 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示 Type 是否声明为公共类型。
public:
property bool IsNotPublic { bool get(); };
public bool IsNotPublic { get; }
member this.IsNotPublic : bool
Public ReadOnly Property IsNotPublic As Boolean
属性值
如果 true
未声明为公共类型且不是嵌套类型,则为 Type;否则为 false
。
实现
示例
此示例使用 IsNotPublic
属性获取类型的可见性。
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
open System
// Get the Type and MemberInfo.
let t = Type.GetType "System.IO.File"
let members = t.GetMembers()
// Get and display the DeclaringType method.
printfn $"\nThere are {members.Length} members in {t.FullName}."
printfn $"Is {t.FullName} non-public? {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
下面的代码示例演示了为什么不能 IsPublic
将 和 IsNotPublic
用于嵌套类。
public ref class A
{
public:
ref class B{};
private:
ref class C{};
};
public class A
{
public class B { }
private class C { }
}
module A =
type B() = class end
type C() = class end
Public Class A
Public Class B
End Class
Private Class C
End Class
End Class
对于嵌套类,请忽略 和 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
。