Description





Description

This enumeration is used by the FieldInfo.Attributes property.

Example

using System;

using System.Reflection;



/// <summary>

/// This example gets all the fields from an example class and

/// shows how to interpret the fields attributes.

/// </summary>



public class FieldAttributesSample

{

    public static void Main()

    {

        foreach (FieldInfo field in typeof(DemoClass).GetFields(

            BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static |

            BindingFlags.Instance))

        {

            Console.Write("{0,-25}    {1,-10}", "'" + field.Name + "'",

                GetPrettyAccessibilityName(field.Attributes));

            Console.Write("    ");

            if ((field.Attributes & FieldAttributes.InitOnly) != 0)

            {

                Console.Write ("readonly ");

            }

            if ((field.Attributes & FieldAttributes.Literal) != 0)

            {

                Console.Write("literal ");

            }

            if ((field.Attributes & FieldAttributes.NotSerialized) != 0)

            {

                Console.Write("[NonSerialized]");

            }

            if ((field.Attributes & FieldAttributes.Static) != 0)

            {

                Console.Write("static ");

            }

            Console.WriteLine();

        }



        Console.WriteLine();

        Console.WriteLine();

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

        Console.ReadLine();

    }

    private static string GetPrettyAccessibilityName(FieldAttributes attributes)

    {

        switch (attributes & FieldAttributes.FieldAccessMask)

        {

            case FieldAttributes.Assembly:

                return "internal";



            case FieldAttributes.Family:

                return "protected";



            case FieldAttributes.Public:

                return "public";



            case FieldAttributes.Private:

                return "private";



            default:

                return (attributes & FieldAttributes.FieldAccessMask).ToString();

        }

    }

}

[Serializable]

class DemoClass

{

    public readonly int ReadonlyField = 29;

    public const int ConstField= 2;

    [NonSerialized] public int NonSerializableValue = 42;

    public static readonly int StaticReadonlyField = 29;

    protected static int FamilyStaticField = 33;

    private static int PrivateField = 7;



}


The output is


'ReadonlyField'              public        readonly

'NonSerializableValue'       public        [NonSerialized]

'StaticReadonlyField'        public        readonly static

'FamilyStaticField'          protected     static

'PrivateField'               private       static

'ConstField'                 public        literal static





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows