FieldInfo.SetValue 메서드

정의

지정된 값에 대한 개체의 필드 값을 설정합니다.

오버로드

SetValue(Object, Object)

지정된 개체에서 지원하는 필드 값을 설정합니다.

SetValue(Object, Object, BindingFlags, Binder, CultureInfo)

파생 클래스에 재정의할 때 지정된 개체에서 지원하는 필드 값을 설정합니다.

SetValue(Object, Object)

지정된 개체에서 지원하는 필드 값을 설정합니다.

public:
 virtual void SetValue(System::Object ^ obj, System::Object ^ value);
public:
 void SetValue(System::Object ^ obj, System::Object ^ value);
public virtual void SetValue (object obj, object value);
public void SetValue (object? obj, object? value);
public void SetValue (object obj, object value);
abstract member SetValue : obj * obj -> unit
override this.SetValue : obj * obj -> unit
member this.SetValue : obj * obj -> unit
Public Overridable Sub SetValue (obj As Object, value As Object)
Public Sub SetValue (obj As Object, value As Object)

매개 변수

obj
Object

필드 값이 설정될 개체입니다.

value
Object

필드에 할당할 값입니다.

구현

예외

호출자에게 이 필드에 액세스할 수 있는 권한이 없는 경우

참고: Windows 스토어 앱 또는 이식 가능한 클래스 라이브러리용 .NET에서 기본 클래스 예외MemberAccessException를 대신 catch합니다.

obj 매개 변수가 null이고 필드는 인스턴스 필드인 경우

참고: .NET for Windows Store 앱 또는 이식 가능한 클래스 라이브러리에서 대신 catch Exception 합니다.

필드가 이벤트에 없는 경우

또는 value 매개 변수를 변환하고 필드에 저장할 수 없는 경우

예제

다음은 필드 값을 설정하고, 값을 가져오고 표시하고, 필드를 수정하고, 결과를 표시하는 예제입니다.

using namespace System;
using namespace System::Reflection;
using namespace System::Globalization;

public ref class Example
{
private:
   String^ myString;

public:
   Example()
   {
      myString = "Old value";
   }

   property String^ StringProperty 
   {
      String^ get()
      {
         return myString;
      }
   }
};

int main()
{
    Example^ myObject = gcnew Example;
    Type^ myType = Example::typeid;
    FieldInfo^ myFieldInfo = myType->GetField( "myString", 
        BindingFlags::NonPublic | BindingFlags::Instance);
      
    // Display the string before applying SetValue to the field.
    Console::WriteLine( "\nThe field value of myString is \"{0}\".", 
        myFieldInfo->GetValue( myObject ) );
    // Display the SetValue signature used to set the value of a field.
    Console::WriteLine( "Applying SetValue(Object, Object)." );

    // Change the field value using the SetValue method. 
    myFieldInfo->SetValue( myObject, "New value" );     
    // Display the string after applying SetValue to the field.
    Console::WriteLine( "The field value of mystring is \"{0}\".", 
        myFieldInfo->GetValue(myObject));
}
/* This code produces the following output:

The field value of myString is "Old value".
Applying SetValue(Object, Object).
The field value of mystring is "New value".
 */
using System;
using System.Reflection;
using System.Globalization;

public class Example
{
    private string myString;
    public Example()
    {
        myString = "Old value";
    }

    public string StringProperty
    {
        get
        {
            return myString;
        }
    }
}

public class FieldInfo_SetValue
{
    public static void Main()
    {
        Example myObject = new Example();
        Type myType = typeof(Example);
        FieldInfo myFieldInfo = myType.GetField("myString",
            BindingFlags.NonPublic | BindingFlags.Instance);

        // Display the string before applying SetValue to the field.
        Console.WriteLine( "\nThe field value of myString is \"{0}\".",
        myFieldInfo.GetValue(myObject));
        // Display the SetValue signature used to set the value of a field.
        Console.WriteLine( "Applying SetValue(Object, Object).");

        // Change the field value using the SetValue method.
        myFieldInfo.SetValue(myObject, "New value");
        // Display the string after applying SetValue to the field.
        Console.WriteLine( "The field value of mystring is \"{0}\".",
            myFieldInfo.GetValue(myObject));
    }
}

/* This code example produces the following output:

The field value of myString is "Old value".
Applying SetValue(Object, Object).
The field value of mystring is "New value".
 */
Imports System.Reflection
Imports System.Globalization

Public Class Example
   Private myString As String
   
   Public Sub New()
      myString = "Old value"
   End Sub 
   
   ReadOnly Property StringProperty() As String
      Get
         Return myString
      End Get
   End Property
End Class 


Public Module FieldInfo_SetValue
   
   Sub Main()

        Dim myObject As New Example()
        Dim myType As Type = GetType(Example)
        Dim myFieldInfo As FieldInfo = myType.GetField("myString", _
           BindingFlags.NonPublic Or BindingFlags.Instance)

        ' Display the string before applying SetValue to the field.
        Console.WriteLine(vbCrLf & "The field value of myString is ""{0}"".", _
            myFieldInfo.GetValue(myObject))
        ' Display the SetValue signature used to set the value of a field.
        Console.WriteLine("Applying SetValue(Object, Object).")

        ' Change the field value using the SetValue method. 
        myFieldInfo.SetValue(myObject, "New value")
        ' Display the string after applying SetValue to the field.
        Console.WriteLine("The field value of mystring is ""{0}"".", _
            myFieldInfo.GetValue(myObject))

    End Sub 
End Module

' This code example produces the following output:
' The field value of myString is "Old value".
' Applying SetValue(Object, Object).
' The field value of mystring is "New value".

설명

이 메서드는 개체에서 이 인스턴스에 의해 반영된 필드에 할당 value 됩니다 obj. 필드가 정적 obj 이면 무시됩니다. 비정적 필드의 경우 필드를 obj 상속하거나 선언하는 클래스의 인스턴스여야 합니다. 새 값이 .로 Object전달됩니다. 예를 들어 필드의 형식이 부울이면 적절한 부울 값이 Object 있는 인스턴스가 전달됩니다. 값을 SetValue 설정하기 전에 사용자에게 액세스 권한이 있는지 확인합니다. 이 최종 메서드는 다음 SetValue 메서드를 호출하기 위한 편리한 메서드입니다.

이 메서드는 정적 init 전용(readonly C#) 필드의 값을 안정적으로 설정하는 데 사용할 수 없습니다. .NET Core 3.0 이상 버전에서는 정적 초기화 전용 필드에 값을 설정하려고 하면 예외가 throw됩니다.

참고

완전히 신뢰할 수 있는 코드에는 리플렉션을 사용하여 프라이빗 생성자, 메서드, 필드 및 속성에 액세스하고 호출하는 데 필요한 권한이 있습니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 메서드를 사용하여 호출자에게 플래그가 ReflectionPermissionFlag.RestrictedMemberAccess 부여된 ReflectionPermission 경우 및 public이 아닌 멤버의 권한 부여 집합이 호출자의 권한 부여 집합 또는 해당 하위 집합으로 제한되는 경우 이 메서드를 사용하여 공용이 아닌 멤버에 액세스할 수 있습니다. ( 리플렉션에 대한 보안 고려 사항 참조)

이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상

SetValue(Object, Object, BindingFlags, Binder, CultureInfo)

파생 클래스에 재정의할 때 지정된 개체에서 지원하는 필드 값을 설정합니다.

public:
 abstract void SetValue(System::Object ^ obj, System::Object ^ value, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, System::Globalization::CultureInfo ^ culture);
public abstract void SetValue (object? obj, object? value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, System.Globalization.CultureInfo? culture);
public abstract void SetValue (object obj, object value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Globalization.CultureInfo culture);
abstract member SetValue : obj * obj * System.Reflection.BindingFlags * System.Reflection.Binder * System.Globalization.CultureInfo -> unit
Public MustOverride Sub SetValue (obj As Object, value As Object, invokeAttr As BindingFlags, binder As Binder, culture As CultureInfo)

매개 변수

obj
Object

필드 값이 설정될 개체입니다.

value
Object

필드에 할당할 값입니다.

invokeAttr
BindingFlags

필요한 바인딩 형식을 지정하는 Binder 필드(예: Binder.CreateInstance 또는 Binder.ExactBinding)입니다.

binder
Binder

리플렉션을 통해 바인딩, 인수의 형식 변환 및 멤버 호출을 가능하게 하는 속성 집합입니다. bindernull이면 Binder.DefaultBinding가 사용됩니다.

culture
CultureInfo

특정 문화권의 소프트웨어 기본 설정입니다.

구현

예외

호출자에게 이 필드에 액세스할 수 있는 권한이 없는 경우

obj 매개 변수가 null이고 필드는 인스턴스 필드인 경우

필드가 이벤트에 없는 경우

또는 value 매개 변수를 변환하고 필드에 저장할 수 없는 경우

설명

이 메서드는 이 인스턴스가 반영하는 필드에 할당 value 됩니다 obj. 필드가 정적 obj 이면 무시됩니다. 비정적 필드의 경우 필드를 obj 상속하거나 선언하는 클래스의 인스턴스여야 합니다. The new value is passed as an Object. 예를 들어 필드의 형식이면 Boolean적절한 부울 값을 가진 인스턴스 Object 가 전달됩니다. 값을 SetValue 설정하기 전에 사용자에게 액세스 권한이 있는지 확인합니다.

이 메서드는 정적 init-only(readonly C#) 필드의 값을 안정적으로 설정하는 데 사용할 수 없습니다. .NET Core 3.0 이상 버전에서는 정적 init 전용 필드에 값을 설정하려고 하면 예외가 throw됩니다.

참고

완전히 신뢰할 수 있는 코드에는 리플렉션을 사용하여 프라이빗 생성자, 메서드, 필드 및 속성에 액세스하고 호출하는 데 필요한 권한이 있습니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 메서드를 사용하여 호출자에게 플래그가 ReflectionPermissionFlag.RestrictedMemberAccess 부여된 ReflectionPermission 경우 및 비공개 멤버의 권한 부여 집합이 호출자의 권한 부여 집합 또는 해당 하위 집합으로 제한되는 경우 이 메서드를 사용하여 비공개 멤버에 액세스할 수 있습니다. ( 리플렉션에 대한 보안 고려 사항 참조)

이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상