FieldInfo.IsPrivate 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,通过该值指示此字段是否为私有字段。
public:
property bool IsPrivate { bool get(); };
public bool IsPrivate { get; }
member this.IsPrivate : bool
Public ReadOnly Property IsPrivate As Boolean
属性值
如果此字段为私有字段,则为 true
;否则为 false
。
实现
示例
以下示例返回一个值,该值指示 类的 字段是否为私有。
using namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
String^ myField;
public:
array<String^>^myArray;
MyClass()
{
myField = "Microsoft";
array<String^>^s = {"New York","New Jersey"};
myArray = s;
}
property String^ GetField
{
String^ get()
{
return myField;
}
}
};
int main()
{
try
{
// Gets the type of MyClass.
Type^ myType = MyClass::typeid;
// Gets the field information of MyClass.
array<FieldInfo^>^myFields = myType->GetFields( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Public | BindingFlags::Instance) );
Console::WriteLine( "\nDisplaying whether the fields of {0} are private or not:\n", myType );
for ( int i = 0; i < myFields->Length; i++ )
{
// Check whether the field is private or not.
if ( myFields[ i ]->IsPrivate )
Console::WriteLine( " {0} is a private field.", myFields[ i ]->Name );
else
Console::WriteLine( " {0} is not a private field.", myFields[ i ]->Name );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0} ", e->Message );
}
}
using System;
using System.Reflection;
class MyClass
{
private string myField;
public string[] myArray = new string[] {"New York", "New Jersey"};
MyClass()
{
myField = "Microsoft";
}
string GetField
{
get
{
return myField;
}
}
}
class FieldInfo_IsPrivate
{
public static void Main()
{
try
{
// Gets the type of MyClass.
Type myType = typeof(MyClass);
// Gets the field information of MyClass.
FieldInfo[] myFields = myType.GetFields(BindingFlags.NonPublic
|BindingFlags.Public
|BindingFlags.Instance);
Console.WriteLine("\nDisplaying whether the fields of {0} are private or not:\n", myType);
for(int i = 0; i < myFields.Length; i++)
{
// Check whether the field is private or not.
if(myFields[i].IsPrivate)
Console.WriteLine("{0} is a private field.", myFields[i].Name);
else
Console.WriteLine("{0} is not a private field.", myFields[i].Name);
}
}
catch(Exception e)
{
Console.WriteLine("Exception : {0} " , e.Message);
}
}
}
Imports System.Reflection
Class [MyClass]
Private myField As String
Public myArray() As String = {"New York", "New Jersey"}
Sub New()
myField = "Microsoft"
End Sub
ReadOnly Property GetField() As String
Get
Return myField
End Get
End Property
End Class
Class FieldInfo_IsPrivate
Public Shared Sub Main()
Try
' Gets the type of MyClass.
Dim myType As Type = GetType([MyClass])
' Gets the field information of MyClass.
Dim myFields As FieldInfo() = myType.GetFields((BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance))
Console.WriteLine(ControlChars.Cr & "Displaying whether the fields of {0} are private or not:" & ControlChars.Cr, myType)
Console.WriteLine()
Dim i As Integer
For i = 0 To myFields.Length - 1
' Check whether the field is private or not.
If myFields(i).IsPrivate Then
Console.WriteLine("{0} is a private field.", myFields(i).Name)
Else
Console.WriteLine("{0} is not a private field.", myFields(i).Name)
End If
Next i
Catch e As Exception
Console.WriteLine("Exception : {0} ", e.Message.ToString())
End Try
End Sub
End Class
注解
私有字段只能从成员函数访问。
属性 IsPrivate
是在设置 属性时设置的 FieldAttributes.Private
。
若要获取 IsPrivate
属性,请首先获取类 Type
。
Type
从 中获取 FieldInfo
。 从 中 FieldInfo
获取 IsPrivate
属性。 若要访问非公共字段,请在 BindingFlags
方法中GetField
将 NonPublic
设置为 ,并将 Static
设置为 或 Instance
。