FieldInfo.IsInitOnly Propiedad

Definición

Obtiene un valor que indica si el campo sólo se puede establecer en el cuerpo del constructor.

C#
public bool IsInitOnly { get; }

Valor de propiedad

Es true si el campo tiene establecido el atributo InitOnly; en caso contrario, es false.

Implementaciones

Ejemplos

En el ejemplo siguiente, se crean dos campos. El segundo campo es de solo lectura, sin descriptor de acceso set y IsInitOnly se establece en true.

C#
using System;
using System.Reflection;

//Make two fields, one public and one read-only.
public class Myfielda
{
    public string field = "A - public modifiable field";
}
public class Myfieldb
{
    public readonly string field = "B - readonly field";
}

public class Myfieldinfo
{
    public static int Main()
    {
        Console.WriteLine("\nReflection.FieldInfo");
        Myfielda Myfielda = new Myfielda();
        Myfieldb Myfieldb = new Myfieldb();

        //Get the Type and FieldInfo.
        Type MyTypea = typeof(Myfielda);
        FieldInfo Myfieldinfoa = MyTypea.GetField("field",
            BindingFlags.Public | BindingFlags.Instance);
        Type MyTypeb = typeof(Myfieldb);
        FieldInfo Myfieldinfob = MyTypeb.GetField("field",
            BindingFlags.Public | BindingFlags.Instance);

        //Modify the fields.
        //Note that Myfieldb is not modified, as it is
        //read-only (IsInitOnly is True).
        Myfielda.field = "A - modified";
        //Myfieldb.field = "B - modified";

        //For the first field, get and display the name, field, and IsInitOnly state.
        Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
            MyTypea.FullName,
            Myfieldinfoa.GetValue(Myfielda),
            Myfieldinfoa.IsInitOnly);

        //For the second field get and display the name, field, and IsInitOnly state.
        Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
            MyTypeb.FullName,
            Myfieldinfob.GetValue(Myfieldb),
            Myfieldinfob.IsInitOnly);

        return 0;
    }
}

Este código genera el siguiente resultado:

Consola
Reflection.FieldInfo

Myfielda - A- modified, IsInitOnly = False

Myfieldb - B readonly field, IsInitOnly = True

Comentarios

Si el valor devuelto es true, el campo solo se puede inicializar y es de solo lectura a partir de entonces.

Para obtener la IsInitOnly propiedad , obtenga primero la clase Type. TypeEn , obtenga .FieldInfo En , FieldInfoobtenga la IsInitOnly propiedad . Para acceder a un campo no público, combine BindingFlags.NonPublic con o con o con BindingFlags.Static y BindingFlags.Instance en el GetField método .

La IsInitOnly propiedad se establece cuando se establece el FieldAttributes.InitOnly atributo .

Se aplica a

Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Consulte también