Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetFields Method (BindingFlags)

.NET Framework 1.1

When overridden in a derived class, searches for the fields defined for the current Type, using the specified binding constraints.

[Visual Basic]
Overloads Public MustOverride Function GetFields( _
   ByVal bindingAttr As BindingFlags _
) As FieldInfo() Implements IReflect.GetFields
[C#]
public abstract FieldInfo[] GetFields(
 BindingFlags bindingAttr
);
[C++]
public: virtual FieldInfo* GetFields(
 BindingFlags bindingAttr
) [] = 0;
[JScript]
public abstract function GetFields(
   bindingAttr : BindingFlags
) : FieldInfo[];

Parameters

bindingAttr
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

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

IReflect.GetFields

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 can vary.

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 and protected fields) in the search.
  • Specify BindingFlags.FlattenHierarchy to include static members up the hierarchy.

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 requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

Example

The following example shows a use of the GetFields(BindingFlags) method.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class AttributesSample

    Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
        str2m = "in Mymethod"
    End Sub 'Mymethod

    Public Shared Function Main(ByVal args() As String) As Integer
        Console.WriteLine("Reflection.MethodBase.Attributes Sample")

        ' Get the type.
        Dim MyType As Type = Type.GetType("AttributesSample")

        ' Get the method Mymethod on the type.
        Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod")

        ' Display the method name.
        Console.WriteLine("Mymethodbase = {0}.", Mymethodbase)

        ' Get the MethodAttribute enumerated value.
        Dim Myattributes As MethodAttributes = Mymethodbase.Attributes

        ' Display the flags that are set.
        PrintAttributes(GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
        Return 0
    End Function 'Main

    Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer)
        If Not attribType.IsEnum Then
            Console.WriteLine("This type is not an enum.")
            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
                Console.WriteLine(fields(i).Name)
            End If
        Next i
    End Sub 'PrintAttributes
End Class 'AttributesSample

[C#] 

using System;
using System.Reflection;
 
class AttributesSample
{
    public void Mymethod (int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }
 
    public static int Main(string[] args)
    {      
        Console.WriteLine ("Reflection.MethodBase.Attributes Sample");
       
        // Get the type.
        Type MyType = Type.GetType("AttributesSample");
 
        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");
 
        // Display the method name.
        Console.WriteLine("Mymethodbase = " + Mymethodbase);
 
        // Get the MethodAttribute enumerated value.
        MethodAttributes Myattributes = Mymethodbase.Attributes;
 
        // Display the flags that are set.
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }
 
 
    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        if (!attribType.IsEnum)
        { 
            Console.WriteLine("This type is not an enum."); 
            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)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}

[C++] 

#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

public __gc class AttributesSample
{
public:
    void Mymethod (int int1m, [Out] String** str2m, String** str3m)
    {
        *str2m = S"in Mymethod";
    }
};

void PrintAttributes(Type* attribType, int iAttribValue)
{
    if (!attribType->IsEnum)
    { 
        Console::WriteLine(S"This type is not an enum."); 
        return; 
    }

    FieldInfo* fields[] =
        attribType->GetFields(static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static));
    for (int i = 0; i < fields->Length; i++)
    {
        int fieldvalue = *dynamic_cast<Int32*>(fields[i]->GetValue(0));
        if ((fieldvalue & iAttribValue) == fieldvalue)
        {
            Console::WriteLine(fields[i]->Name);
        }
    }
}

int main()
{      
    Console::WriteLine (S"Reflection.MethodBase.Attributes Sample");

    // Get the type.
    Type* MyType = Type::GetType(S"AttributesSample");

    // Get the method Mymethod on the type.
    MethodBase* Mymethodbase = MyType->GetMethod(S"Mymethod");

    // Display the method name.
    Console::WriteLine(S"Mymethodbase = {0}", Mymethodbase);

    // Get the MethodAttribute enumerated value.
    MethodAttributes Myattributes = Mymethodbase->Attributes;

    // Display the flags that are set.
    PrintAttributes(__typeof(System::Reflection::MethodAttributes), (int) Myattributes);
    return 0;
}

[JScript] 

 import System;
 import System.Reflection;
 
 class AttributesSample
 {
    public function Mymethod (int1m : int) : void
    {
    }
 
    public static function Main() : void
    {      
       Console.WriteLine ("Reflection.MethodBase.Attributes Sample");
       
       // Get our type
       var MyType : Type = Type.GetType("AttributesSample");
 
       // Get the method Mymethod on our type
       var Mymethodbase : MethodBase = MyType.GetMethod("Mymethod");
 
       // Print out the method
       Console.WriteLine("Mymethodbase = " + Mymethodbase);
 
       // Get the MethodAttribute enumerated value
       var Myattributes : MethodAttributes = Mymethodbase.Attributes;
 
       // print out the flags set
       PrintAttributes( System.Reflection.MethodAttributes, int(Myattributes) );
    }
 
 
    public static function PrintAttributes( attribType : Type, iAttribValue : int ) : void 
    {
       if ( ! attribType.IsEnum ) { Console.WriteLine( "This type is not an enum" ); return; }
 
       var fields : FieldInfo[] = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
       for ( var i:int = 0; i < fields.Length; i++ )
       {
          var fieldvalue : int = int(fields[i].GetValue(null));
          if ( (fieldvalue & iAttribValue) == fieldvalue )
          {
             Console.WriteLine( "\t" + fields[i].Name );
          }
       }
    }
 }
 AttributesSample.Main();
/* 
 This code produces the following output:
 
Reflection.MethodBase.Attributes Sample
Mymethodbase = Void Mymethod(Int32)
        PrivateScope
        FamANDAssem
        Family
        Public
        Virtual
        HideBySig
        VtableLayoutMask
        ReuseSlot
        NewSlot
*/

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetFields Overload List | FieldInfo | BindingFlags | DefaultBinder | ReflectionPermission | GetField

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.