다음을 통해 공유


FieldInfo.MemberType Property

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

Gets a value that indicates that this member is a field.

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

Syntax

'Declaration
Public Overrides ReadOnly Property MemberType As MemberTypes
public override MemberTypes MemberType { get; }

Property Value

Type: System.Reflection.MemberTypes
A value that indicates that this member is a field.

Exceptions

Exception Condition
MethodAccessException

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

Remarks

This property overrides MemberType. Therefore, when you examine a set of MemberInfo objects — for example, the array returned by GetMembers — the MemberType property returns MemberTypes.Field only when a given member is a field.

Examples

The following example determines whether each instance member of the Example class is a field and displays the result.

Imports System.Reflection

' Make a field.
Public Class Example

   Private m_field As String = "a private field"

   Public ReadOnly Property Field() As String
      Get
         Return m_field
      End Get
   End Property

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

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

      Dim instanceMembers() As MemberInfo = _
         GetType(Example).GetMembers(BindingFlags.Public Or BindingFlags.NonPublic Or _
                                     BindingFlags.Instance)
      For Each mi As MemberInfo In instanceMembers
         If mi.MemberType = MemberTypes.Field Then
            outputBlock.Text += String.Format("{0} is a field. <----" & vbLf, mi.Name)
         Else
            outputBlock.Text += String.Format("{0} is not a field. " & vbLf, mi.Name)
         End If
      Next mi

   End Sub
End Class

' This example produces the following output:
'

'Reflection.FieldInfo
'
'get_Field is not a field. 
'ToString is not a field. 
'Equals is not a field. 
'GetHashCode is not a field. 
'GetType is not a field. 
'Finalize is not a field. 
'MemberwiseClone is not a field. 
'.ctor is not a field. 
'Field is not a field. 
'm_field is a field. <----
using System;
using System.Reflection;

// Make a field.
public class Example
{
   private string m_field = "a private field";

   public string Field   
   {
      get
      {
         return m_field;
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Reflection.FieldInfo\n\n";

      MemberInfo[] instanceMembers = 
         typeof(Example).GetMembers(BindingFlags.Public | BindingFlags.NonPublic | 
                                    BindingFlags.Instance);
      foreach( MemberInfo mi in instanceMembers )
      {
         if (mi.MemberType == MemberTypes.Field)
         {
            outputBlock.Text += String.Format("{0} is a field. <----\n", mi.Name);
         }
         else
         {
            outputBlock.Text += String.Format("{0} is not a field. \n", mi.Name);
         }
      }
   }
}

/* This example produces the following output:

Reflection.FieldInfo

get_Field is not a field. 
ToString is not a field. 
Equals is not a field. 
GetHashCode is not a field. 
GetType is not a field. 
Finalize is not a field. 
MemberwiseClone is not a field. 
.ctor is not a field. 
Field is not a field. 
m_field is a field. <----
 */

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.