FieldInfo.IsInitOnly 속성

정의

필드가 생성자의 본문에만 적용될 수 있는지 여부를 나타내는 값을 가져옵니다.Gets a value indicating whether the field can only be set in the body of the constructor.

public:
 property bool IsInitOnly { bool get(); };
public bool IsInitOnly { get; }
member this.IsInitOnly : bool
Public ReadOnly Property IsInitOnly As Boolean

속성 값

필드에 true 특성 집합이 있으면 InitOnly이고, 그렇지 않으면 false입니다.true if the field has the InitOnly attribute set; otherwise, false.

구현

예제

다음 예제에서는 두 개의 필드가 만들어집니다.In the following example, two fields are created. 두 번째 필드는 읽기 전용 이며 set 접근자가 없고 IsInitOnly true로 설정 됩니다.The second field is read-only, having no set accessor, and IsInitOnly is set to true.

using namespace System;
using namespace System::Reflection;

//Make two fields, one public and one read-only.
public ref class Myfielda
{
public:
   String^ field;
   Myfielda()
      : field( "A - public field" )
   {}


   property String^ Field 
   {
      String^ get()
      {
         return field;
      }

      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }

   }

};

public ref class Myfieldb
{
private:
   String^ const field;

public:
   Myfieldb()
      : field( "B - readonly field" )
   {}


   property String^ Field 
   {
      String^ get()
      {
         return field;
      }

   }

};

int main()
{
   Console::WriteLine( "\nReflection.FieldInfo" );
   Myfielda^ myfielda = gcnew Myfielda;
   Myfieldb^ myfieldb = gcnew Myfieldb;
   
   //Get the Type and FieldInfo.
   Type^ MyTypea = Type::GetType( "Myfielda" );
   FieldInfo^ Myfieldinfoa = MyTypea->GetField( "field", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
   Type^ MyTypeb = Type::GetType( "Myfieldb" );
   FieldInfo^ Myfieldinfob = MyTypeb->GetField( "field", static_cast<BindingFlags>(BindingFlags::NonPublic | 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;
}


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;
    }
}
Imports System.Reflection

'Make two fields, one public and one read-only.
Public Class Myfielda
    Public field As String = "A - public modifiable field"
End Class

Public Class Myfieldb
    Public ReadOnly field As String = "B - readonly field"
End Class

Public Class Myfieldinfo
    Public Shared Function Main() As Integer
        Console.WriteLine("Reflection.FieldInfo")
        Console.WriteLine()
        Dim Myfielda As New Myfielda()
        Dim Myfieldb As New Myfieldb()

        'Get the Type and FieldInfo.
        Dim MyTypea As Type = GetType(Myfielda)
        Dim Myfieldinfoa As FieldInfo = MyTypea.GetField("field", _
            BindingFlags.Public Or BindingFlags.Instance)
        Dim MyTypeb As Type = GetType(Myfieldb)
        Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("field", _
            BindingFlags.Public Or BindingFlags.Instance)

        'Modify the fields.
        'Note that Myfieldb is not modified, as it is
        'read-only (IsInitOnly is True).
        Myfielda.field = "A - modified"
        'For the first field, get and display the name, field, and IsInitOnly state.
        Console.WriteLine("{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.WriteLine("{0} - {1}, IsInitOnly = {2} ", MyTypeb.FullName, _
            Myfieldinfob.GetValue(Myfieldb), Myfieldinfob.IsInitOnly)

        Return 0
    End Function 'Main
End Class

이 코드의 결과는 다음과 같습니다.This code produces the following output:

Reflection.FieldInfoReflection.FieldInfo

-A-Myfielda 수정 IsInitOnly = FalseMyfielda - A- modified, IsInitOnly = False

Myfieldb-B 읽기 전용 필드를 IsInitOnly = TrueMyfieldb - B readonly field, IsInitOnly = True

설명

반환 된 값이 true경우에만 필드를 초기화할 수 있으며 그 이후에는 읽기 전용입니다.If the returned value is true, the field can only be initialized, and is read-only thereafter.

IsInitOnly 속성을 가져오려면 먼저 클래스 Type가져옵니다.To get the IsInitOnly property, first get the class Type. Type에서 FieldInfo를 가져옵니다.From the Type, get the FieldInfo. FieldInfo에서 IsInitOnly 속성을 가져옵니다.From the FieldInfo, get the IsInitOnly property. Public이 아닌 필드에 액세스 하려면 GetField 메서드에서 BindingFlags.StaticBindingFlags.Instance 중 하나 또는 둘 모두를 사용 하 여 BindingFlags.NonPublic를 결합 합니다.To access a non-public field, combine BindingFlags.NonPublic with either or both of BindingFlags.Static and BindingFlags.Instance in the GetField method.

IsInitOnly 속성은 FieldAttributes.InitOnly 특성이 설정 될 때 설정 됩니다.The IsInitOnly property is set when the FieldAttributes.InitOnly attribute is set.

적용 대상

추가 정보