C# TypeInfo GetFields(BindingFlags)

Description

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

Syntax

TypeInfo.GetFields(BindingFlags) has the following syntax.


public abstract FieldInfo[] GetFields(
  BindingFlags bindingAttr
)

Parameters

TypeInfo.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

TypeInfo.GetFields(BindingFlags) method returns

Example

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


/*  ww  w.ja  v  a  2s .  co  m*/
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)
    {      
        Type MyType = Type.GetType("AttributesSample");

        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

        Console.WriteLine("Mymethodbase = " + Mymethodbase);

        MethodAttributes Myattributes = Mymethodbase.Attributes;

        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }
    public static void PrintAttributes(Type attribType, int iAttribValue)
    {

        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.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo