FieldInfo.Attributes Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atribut yang terkait dengan bidang ini.
public:
abstract property System::Reflection::FieldAttributes Attributes { System::Reflection::FieldAttributes get(); };
public abstract System.Reflection.FieldAttributes Attributes { get; }
member this.Attributes : System.Reflection.FieldAttributes
Public MustOverride ReadOnly Property Attributes As FieldAttributes
Nilai Properti
untuk FieldAttributes
bidang ini.
Penerapan
Contoh
Contoh kode berikut membangun tiga bidang dan menampilkan atribut bidangnya. Nilai FieldAttributes
dapat berisi lebih dari satu atribut, seperti Public
dan Literal
, seperti yang ditunjukkan di bidang ketiga.
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;
public ref class Demo
{
private:
// Make three fields:
// The first field is private.
String^ m_field;
// The second field is public.
public:
String^ Field;
// The third field is public and literal.
literal String^ FieldC = "String C";
Demo() { m_field = "String A"; Field = "String B"; }
};
static void DisplayField(Object^ obj, FieldInfo^ f)
{
// Display the field name, value, and attributes.
//
Console::WriteLine("{0} = \"{1}\"; attributes: {2}",
f->Name, f->GetValue(obj), f->Attributes);
};
void main()
{
Console::WriteLine ("\nReflection.FieldAttributes");
Demo^ d = gcnew Demo();
// Get a Type object for Demo, and a FieldInfo for each of
// the three fields. Use the FieldInfo to display field
// name, value for the Demo object in d, and attributes.
//
Type^ myType = Demo::typeid;
FieldInfo^ fiPrivate = myType->GetField("m_field",
BindingFlags::NonPublic | BindingFlags::Instance);
DisplayField(d, fiPrivate);
FieldInfo^ fiPublic = myType->GetField("Field",
BindingFlags::Public | BindingFlags::Instance);
DisplayField(d, fiPublic);
FieldInfo^ fiConstant = myType->GetField("FieldC",
BindingFlags::Public | BindingFlags::Static);
DisplayField(d, fiConstant);
}
/* This code example produces the following output:
Reflection.FieldAttributes
m_field = "String A"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
*/
using System;
using System.Reflection;
public class Demo
{
// Make three fields:
// The first field is private.
private string m_field = "String A";
// The second field is public.
public string Field = "String B";
// The third field is public const (hence also literal and static),
// with a default value.
public const string FieldC = "String C";
}
public class Myfieldattributes
{
public static void Main()
{
Console.WriteLine ("\nReflection.FieldAttributes");
Demo d = new Demo();
// Get a Type object for Demo, and a FieldInfo for each of
// the three fields. Use the FieldInfo to display field
// name, value for the Demo object in d, and attributes.
//
Type myType = typeof(Demo);
FieldInfo fiPrivate = myType.GetField("m_field",
BindingFlags.NonPublic | BindingFlags.Instance);
DisplayField(d, fiPrivate);
FieldInfo fiPublic = myType.GetField("Field",
BindingFlags.Public | BindingFlags.Instance);
DisplayField(d, fiPublic);
FieldInfo fiConstant = myType.GetField("FieldC",
BindingFlags.Public | BindingFlags.Static);
DisplayField(d, fiConstant);
}
static void DisplayField(Object obj, FieldInfo f)
{
// Display the field name, value, and attributes.
//
Console.WriteLine("{0} = \"{1}\"; attributes: {2}",
f.Name, f.GetValue(obj), f.Attributes);
}
}
/* This code example produces the following output:
Reflection.FieldAttributes
m_field = "String A"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
*/
Imports System.Reflection
Public Class Demo
' Declare three fields.
' The first field is private.
Private m_field As String = "String A"
'The second field is public.
Public Field As String = "String B"
' The third field is public and const, hence also static
' and literal with a default value.
Public Const FieldC As String = "String C"
End Class
Module Module1
Sub Main()
' Create an instance of the Demo class.
Dim d As New Demo()
Console.WriteLine(vbCrLf & "Reflection.FieldAttributes")
' Get a Type object for Demo, and a FieldInfo for each of
' the three fields. Use the FieldInfo to display field
' name, value for the Demo object in d, and attributes.
'
Dim myType As Type = GetType(Demo)
Dim fiPrivate As FieldInfo = myType.GetField("m_field", _
BindingFlags.NonPublic Or BindingFlags.Instance)
DisplayField(d, fiPrivate)
Dim fiPublic As FieldInfo = myType.GetField("Field", _
BindingFlags.Public Or BindingFlags.Instance)
DisplayField(d, fiPublic)
Dim fiConstant As FieldInfo = myType.GetField("FieldC", _
BindingFlags.Public Or BindingFlags.Static)
DisplayField(d, fiConstant)
End Sub
Sub DisplayField(ByVal obj As Object, ByVal f As FieldInfo)
' Display the field name, value, and attributes.
'
Console.WriteLine("{0} = ""{1}""; attributes: {2}", _
f.Name, f.GetValue(obj), f.Attributes)
End Sub
End Module
' This code example produces the following output:
'
'm_field = "String A"; attributes: Private
'Field = "String B"; attributes: Public
'FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
Keterangan
Semua anggota memiliki sekumpulan atribut, yang didefinisikan dalam kaitannya dengan jenis anggota tertentu.
FieldAttributes
memberi tahu pengguna apakah bidang ini adalah bidang privat, bidang statis, dan sebagainya.
Untuk mendapatkan Attributes
properti , pertama-tama dapatkan kelas Type
.
Type
Dari , dapatkan FieldInfo
.
FieldInfo
Dari , dapatkan Attributes
.