Type.GetFields Method (BindingFlags)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
When overridden in a derived class, searches for the fields defined for the current Type, using the specified binding constraints.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public MustOverride Function GetFields ( _
bindingAttr As BindingFlags _
) As FieldInfo()
public abstract FieldInfo[] GetFields(
BindingFlags bindingAttr
)
Parameters
- bindingAttr
Type: System.Reflection.BindingFlags
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
-or-
Zero, to return nulla null reference (Nothing in Visual Basic).
Return Value
Type: array<System.Reflection.FieldInfo[]
An array of FieldInfo objects representing all fields defined for the current Type that match the specified binding constraints.
-or-
An empty array of type FieldInfo, if no fields are defined for the current Type, or if none of the defined fields match the binding constraints.
Implements
Remarks
The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies.
The following BindingFlags filter flags can be used to define which fields to include in the search:
You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
Specify BindingFlags.Public to include public fields in the search.
Specify BindingFlags.NonPublic to include non-public fields (that is, private, internal, and protected fields) in the search. Only protected and internal fields on base classes are returned; private fields on base classes are not returned.
Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.
The following BindingFlags modifier flags can be used to change how the search works:
- BindingFlags.DeclaredOnly to search only the fields declared on the Type, not fields that were simply inherited.
See System.Reflection.BindingFlags for more information.
If the current Type represents a constructed generic type, this method returns the FieldInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the public fields of the class constraint.
Examples
The following example shows a use of the GetFields(BindingFlags) method.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Imports System.Reflection
Class Example
Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
str2m = "in Mymethod"
End Sub
Public Shared Function Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) As Integer
outputBlock.Text &= "Reflection.MethodBase.Attributes Sample" & vbCrLf
' Get the type.
Dim MyType As Type = GetType(Example)
' Get the method Mymethod on the type.
Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod")
' Display the method name.
outputBlock.Text += String.Format("Mymethodbase = {0}.", Mymethodbase) & vbCrLf
' Get the MethodAttribute enumerated value.
Dim Myattributes As MethodAttributes = Mymethodbase.Attributes
' Display the flags that are set.
PrintAttributes(outputBlock, GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
Return 0
End Function
Public Shared Sub PrintAttributes(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal attribType As Type, ByVal iAttribValue As Integer)
If Not attribType.IsEnum Then
outputBlock.Text &= "This type is not an enum." & vbCrLf
Return
End If
Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static))
Dim i As Integer
For i = 0 To fields.Length - 1
Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32)
If (fieldvalue And iAttribValue) = fieldvalue Then
outputBlock.Text &= fields(i).Name & vbCrLf
End If
Next i
End Sub
End Class
' This example produces the following output:
'
'Reflection.MethodBase.Attributes Sample
'Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef)
'PrivateScope
'FamANDAssem
'Family
'Public
'ReuseSlot
using System;
using System.Reflection;
class Example
{
public void Mymethod(int int1m, out string str2m, ref string str3m)
{
str2m = "in Mymethod";
}
public static int Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text += "Reflection.MethodBase.Attributes Sample" + "\n";
// Get the type.
Type MyType = typeof(Example);
// Get the method Mymethod on the type.
MethodBase Mymethodbase = MyType.GetMethod("Mymethod");
// Display the method name.
outputBlock.Text += "Mymethodbase = " + Mymethodbase + "\n";
// Get the MethodAttribute enumerated value.
MethodAttributes Myattributes = Mymethodbase.Attributes;
// Display the flags that are set.
PrintAttributes(outputBlock, typeof(System.Reflection.MethodAttributes), (int)Myattributes);
return 0;
}
public static void PrintAttributes(System.Windows.Controls.TextBlock outputBlock, Type attribType, int iAttribValue)
{
if (!attribType.IsEnum)
{
outputBlock.Text += "This type is not an enum." + "\n";
return;
}
FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
for (int i = 0; i < fields.Length; i++)
{
int fieldvalue = (Int32)fields[i].GetValue(null);
if ((fieldvalue & iAttribValue) == fieldvalue)
{
outputBlock.Text += fields[i].Name + "\n";
}
}
}
}
/* This example produces the following output:
Reflection.MethodBase.Attributes Sample
Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef)
PrivateScope
FamANDAssem
Family
Public
HideBySig
ReuseSlot
*/
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.
See Also