Get the name of a field in CSharp

Description

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

Example


using System;//from w w w . j  a  v  a 2  s .co m
using System.Reflection;
using System.ComponentModel.Design;

class FieldInfo_IsSpecialName
{
    public static void Main()
    {     
       Type myType = typeof(ViewTechnology);
       FieldInfo[] myField = myType.GetFields();
       for(int i = 0; i < myField.Length; i++)
       {
           if(myField[i].IsSpecialName)
           {
               Console.WriteLine("The field {0} has a SpecialName attribute.",
                   myField[i].Name);
           }
       }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type