C# Type GetFields(BindingFlags)

Description

Type GetFields(BindingFlags) when overridden in a derived class, searches for the fields defined for the current Type, using the specified binding constraints.

Syntax

Type.GetFields(BindingFlags) has the following syntax.


public abstract FieldInfo[] GetFields(
  BindingFlags bindingAttr
)

Parameters

Type.GetFields(BindingFlags) has the following parameters.

  • bindingAttr - A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
  • bindingAttr - -or-
  • bindingAttr - Zero, to return null.

Returns

Type.GetFields(BindingFlags) method returns

Example

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


/*  ww w. j a v a  2 s  . c  o  m*/
using System;
using System.Reflection;

class AttributesSample
{
    public void Mymethod(int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }

    public static void Main(string[] args)
    {
        Type MyType = Type.GetType("AttributesSample");
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");
        MethodAttributes Myattributes = Mymethodbase.Attributes;
        Type attribType = typeof(System.Reflection.MethodAttributes);
        int iAttribValue = (int)Myattributes;
        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);
            }
        }

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version