Type Summary



Type Summary


public abstract class FieldInfo : MemberInfo

    {

    // Constructors

       protected FieldInfo();



    // Properties

       public abstract FieldAttributes Attributes { get; }

    MS public abstract RuntimeFieldHandle FieldHandle { get; }

       public abstract Type FieldType { get; }

    MS public bool IsAssembly { get; }

    MS public bool IsFamily { get; }

    MS public bool IsFamilyAndAssembly { get; }

    MS public bool IsFamilyOrAssembly { get; }

    MS public bool IsInitOnly { get; }

    MS public bool IsLiteral { get; }

    MS public bool IsNotSerialized { get; }

    MS public bool IsPinvokeImpl { get; }

    MS public bool IsPrivate { get; }

    MS public bool IsPublic { get; }

    MS public bool IsSpecialName { get; }

    MS public bool IsStatic { get; }

    MS public override MemberTypes MemberType { get; }



       // Methods

       MS public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle);

          public abstract object GetValue(object obj);

    MS CF public virtual object GetValueDirect(TypedReference obj);

          public void SetValue(object obj, object value);

          public abstract void SetValue(object obj, object value,

                               BindingFlags invokeAttr, Binder binder,

                               CultureInfo culture);

    MS CF public virtual void SetValueDirect(TypedReference obj, object value);

       }


Example

using System;

using System.Reflection;



/// <summary>

/// This example prints all the fields of a Type in C# syntax.

/// </summary>



public class FieldInfoSample

{

    public static void Main()

    {

        PrintFields (typeof(Int32));

        PrintFields(typeof(DayOfWeek));

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }

    public static void PrintFields (Type type)

    {

        Console.WriteLine("Fields for {0}", type);

        foreach (FieldInfo field in type.GetFields(BindingFlags.NonPublic |

            BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))

        {

            //skip any fields that are not public or protected

            if (!(field.IsPublic || field.IsFamily || field.IsFamilyOrAssembly))

            {

                continue;

            }

            if (field.DeclaringType.IsEnum)

            {

                if (field.IsStatic)

                {

                    Console.Write(field.Name);

                    Console.Write(" = ");

                    Console.Write(Convert.ChangeType(field.GetValue(null),

                        Enum.GetUnderlyingType(type)));

                    Console.WriteLine(',');

                }

            }

            else

            {

                Console.Write(field.IsPublic ? "public " : field.IsFamily ?

                    "protected " : "protected internal ");



                if (field.IsStatic && field.IsLiteral)

                {

                    Console.Write("const ");

                }

                else

                {

                    if (field.IsStatic)

                    {

                        Console.Write("static ");

                    }

                    if (field.IsInitOnly)

                    {

                        Console.Write("readonly ");

                    }

                }

                Console.Write(field.FieldType);

                Console.Write(' ');

                Console.Write(field.Name);

                if (field.IsStatic)

                {

                    Console.Write(" = ");

                    Console.Write(field.GetValue(null));

                }

                Console.WriteLine(';');

            }

        }



    }

} //end class


The output is


Fields for System.Int32

public const System.Int32 MaxValue = 2147483647;

public const System.Int32 MinValue = -2147483648;

Fields for System.DayOfWeek

Sunday = 0,

Monday = 1,

Tuesday = 2,

Wednesday = 3,

Thursday = 4,

Friday = 5,

Saturday = 6,





Press Enter to continue