Get the type of a field in CSharp

Description

The following code shows how to get the type of a field.

Example


//  www .j a va2  s.co m

using System;
using System.Reflection;
public class Myfield
{
    private string field = "private field";
}

public class Myfieldinfo
{
    public static int Main()
    {
        Myfield Myfield = new Myfield();
        Type MyType = typeof(Myfield);
        FieldInfo Myfieldinfo = MyType.GetField("field", BindingFlags.Instance|BindingFlags.NonPublic);

        Console.Write ("\n{0}.", MyType.FullName);
        Console.Write ("{0} - ", Myfieldinfo.Name);
        Console.Write ("{0};", Myfieldinfo.GetValue(Myfield));
        Console.Write ("\nFieldType = {0}", Myfieldinfo.FieldType);
        return 0;
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type