C# FieldInfo IsStatic

Description

FieldInfo IsStatic Gets a value indicating whether the field is static.

Syntax

FieldInfo.IsStatic has the following syntax.


public bool IsStatic { get; }

Example

The following example determines whether the specified field is static and displays the result.


using System;/*  w w w  .ja va  2  s .  c  o  m*/
using System.Reflection;

public class Myfielda
{
    private string field = "A private field";
    public string Field
    {
        get{return field;}
        set{if(field!=value){field=value;}}
    }
}
public class Myfieldb
{
    static string field = "B private static field";
    public string Field 
    {
        get{return field;}
        set{if(field!=value){field=value;}}
    }
}

public class Myfieldinfo
{
    public static int Main()
    {
        Myfielda Myfielda = new Myfielda();
        Myfieldb Myfieldb = new Myfieldb();

        Type MyTypea = typeof(Myfielda);
        FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance);
        Type MyTypeb = typeof(Myfieldb);
        FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Static);

        Console.Write("\n{0} - ", MyTypea.FullName);
        Console.Write("IsStatic - {0}", Myfieldinfoa.IsStatic);

        Console.Write("\n{0} - ", MyTypeb.FullName);
        Console.Write("IsStatic - {0}", Myfieldinfob.IsStatic);
        return 0;
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo