FieldAttributes Enum
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Anger flaggor som beskriver attributen för ett fält.
Den här uppräkningen stöder en bitvis kombination av dess medlemsvärden.
public enum class FieldAttributes
[System.Flags]
public enum FieldAttributes
[System.Flags]
[System.Serializable]
public enum FieldAttributes
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FieldAttributes
[<System.Flags>]
type FieldAttributes =
[<System.Flags>]
[<System.Serializable>]
type FieldAttributes =
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FieldAttributes =
Public Enum FieldAttributes
- Arv
- Attribut
Fält
| Name | Värde | Description |
|---|---|---|
| PrivateScope | 0 | Anger att fältet inte kan refereras till. |
| Private | 1 | Anger att fältet endast är tillgängligt av den överordnade typen. |
| FamANDAssem | 2 | Anger att fältet endast är tillgängligt av undertyper i den här sammansättningen. |
| Assembly | 3 | Anger att fältet är tillgängligt i hela sammansättningen. |
| Family | 4 | Anger att fältet endast är tillgängligt efter typ och undertyper. |
| FamORAssem | 5 | Anger att fältet är tillgängligt av undertyper var som helst, samt i hela den här sammansättningen. |
| Public | 6 | Anger att fältet är tillgängligt för alla medlemmar som det här omfånget är synligt för. |
| FieldAccessMask | 7 | Anger åtkomstnivån för ett visst fält. |
| Static | 16 | Anger att fältet representerar den definierade typen, annars är det per instans. |
| InitOnly | 32 | Anger att fältet endast initieras och endast kan anges i en konstruktors brödtext. |
| Literal | 64 | Anger att fältets värde är en kompileringstidskonstant (statisk eller tidig bindning). Alla försök att ställa in den genererar en FieldAccessException. |
| NotSerialized | 128 | Anger att fältet inte behöver serialiseras när typen fjärransluts. |
| HasFieldRVA | 256 | Anger att fältet har en relativ virtuell adress (RVA). RVA är platsen för metodtexten i den aktuella bilden, som en adress i förhållande till början av bildfilen där den finns. |
| SpecialName | 512 | Anger en särskild metod med namnet som beskriver hur metoden är speciell. |
| RTSpecialName | 1024 | Anger att common language runtime (interna API:er för metadata) ska kontrollera namnkodningen. |
| HasFieldMarshal | 4096 | Anger att fältet innehåller information om marskalkering. |
| PinvokeImpl | 8192 | Reserverad för framtida användning. |
| HasDefault | 32768 | Anger att fältet har ett standardvärde. |
| ReservedMask | 38144 | Reserverat. |
Exempel
I det här exemplet skapas tre fält och FieldAttributes värdena visas. Ett FieldAttributes värde kan innehålla mer än ett attribut, till exempel både Public och Literal, enligt det tredje fältet.
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
Kommentarer
FieldAttributes använder värdet från FieldAccessMask för att maskera endast de delar av attributvärdet som gäller för tillgängligheten. Följande kod avgör till exempel om Attributes den offentliga biten har angetts.
FieldInfo fi = obj.GetType().GetField("field1");
if ((fi.Attributes & FieldAttributes.FieldAccessMask) ==
FieldAttributes.Public)
{
Console.WriteLine("{0:s} is public. Value: {1:d}", fi.Name, fi.GetValue(obj));
}
Dim fi As FieldInfo = obj.GetType().GetField("field1")
If (fi.Attributes And FieldAttributes.FieldAccessMask) = _
FieldAttributes.Public Then
Console.WriteLine("{0:s} is public. Value: {1:d}", fi.Name, fi.GetValue(obj))
End If
Hämta först klassen FieldAttributesför att hämta Type.
TypeFrån hämtar du FieldInfo.
FieldInfoFrån hämtar du Attributes.
Det uppräknade värdet är ett tal som representerar bitvis ELLER för attributen som implementeras i fältet.