Freigeben über


FieldInfo.Attributes Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the attributes that are associated with this field.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride ReadOnly Property Attributes As FieldAttributes
public abstract FieldAttributes Attributes { get; }

Property Value

Type: System.Reflection.FieldAttributes
The attributes for this field.

Exceptions

Exception Condition
MethodAccessException

This member is invoked late-bound through mechanisms such as Type.InvokeMember.

Remarks

All members have a set of attributes, which are defined in relation to the specific type of member. FieldAttributes informs the user whether this field is a private field, a static field, and so on.

To get the Attributes property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the Attributes.

Examples

The following example builds three fields and displays their field attributes. A FieldAttributes value can contain more than one attribute, such as both Public and Literal, as shown in the third field.

Imports System.Reflection

Public Class Example
   ' 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"


   ' Make the output TextBlock visible to all Shared members.
   Private Shared outputBlock As System.Windows.Controls.TextBlock

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      ' Create an instance of the Example class.
      Dim ex As New Example()

      outputBlock.Text &= vbCrLf & "Reflection.FieldAttributes" & vbCrLf

      ' Get a Type object for Example, and a FieldInfo for each of
      ' the three fields. Use the FieldInfo to display field
      ' name, value for the Example object in d, and attributes.
      '
      Dim myType As Type = GetType(Example)

      Dim fiPrivate As FieldInfo = myType.GetField("m_field", _
          BindingFlags.NonPublic Or BindingFlags.Instance)
      DisplayField(ex, fiPrivate)

      Dim fiPublic As FieldInfo = myType.GetField("Field", _
          BindingFlags.Public Or BindingFlags.Instance)
      DisplayField(ex, fiPublic)

      Dim fiConstant As FieldInfo = myType.GetField("FieldC", _
          BindingFlags.Public Or BindingFlags.Static)
      DisplayField(ex, fiConstant)
   End Sub

   Shared Sub DisplayField(ByVal obj As Object, ByVal f As FieldInfo)

      Dim value As Object = "<not accessible>"
      If f.IsPublic Then
         value = f.GetValue(obj)
      End If

      ' Display the field name, value, and attributes.
      '
      outputBlock.Text &= String.Format("{0} = ""{1}""; attributes: {2}", _
          f.Name, value.ToString(), f.Attributes) & vbCrLf
   End Sub

End Class

' This code example produces the following output:
'
'm_field = "<not accessible>"; attributes: Private
'Field = "String B"; attributes: Public
'FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
using System;
using System.Reflection;

public class Example
{
   // 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";


   // Make the output TextBlock visible to all Shared members.
   static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      outputBlock.Text += "\nReflection.FieldAttributes\n";
      Example ex = new Example();

      // Get a Type object for Example, and a FieldInfo for each of
      // the three fields. Use the FieldInfo to display field
      // name, value for the Example object in d, and attributes.
      //
      Type myType = typeof(Example);
      FieldInfo fiPrivate = myType.GetField("m_field",
          BindingFlags.NonPublic | BindingFlags.Instance);
      DisplayField(ex, fiPrivate);

      FieldInfo fiPublic = myType.GetField("Field",
          BindingFlags.Public | BindingFlags.Instance);
      DisplayField(ex, fiPublic);

      FieldInfo fiConstant = myType.GetField("FieldC",
          BindingFlags.Public | BindingFlags.Static);
      DisplayField(ex, fiConstant);
   }

   static void DisplayField(Object obj, FieldInfo f)
   {
      object value = "<not accessible>";
      if (f.IsPublic)
      {
         value = f.GetValue(obj);
      }

      // Display the field name, value, and attributes.
      //
      outputBlock.Text += String.Format("{0} = \"{1}\"; attributes: {2}\n",
          f.Name, value.ToString(), f.Attributes);
   }
}

/* This code example produces the following output:

Reflection.FieldAttributes
m_field = "<not accessible>"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.