FieldInfo.SetValue 方法

定義

將指定物件的欄位值設為指定的值。

多載

SetValue(Object, Object)

設定指定物件所支援的欄位值。

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

當在衍生類別中覆寫時,請設定由指定物件所支援的欄位值。

SetValue(Object, Object)

來源:
FieldInfo.cs
來源:
FieldInfo.cs
來源:
FieldInfo.cs

設定指定物件所支援的欄位值。

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

obj 參數是 null,且欄位為執行個體欄位。

注意:在 適用于 Windows 市集應用程式的 .NET可攜式類別庫中,改為攔截 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 方法的便利方法。

這個方法無法用來可靠地在 C#) 欄位中設定靜態、僅限 init 的 (readonly 值。 在 .NET Core 3.0 和更新版本中,如果您嘗試在僅限靜態的 init 欄位上設定值,就會擲回例外狀況。

注意

完全信任的程式碼具有使用反映來存取及叫用私人建構函式、方法、欄位和屬性所需的許可權。

注意

從 .NET Framework 2.0 Service Pack 1 開始,如果呼叫端已使用 旗標授 ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess 與呼叫端,而且非公用成員的授與集限制為呼叫端的授與集,或子集,則這個方法可用來存取非公用成員。 (請參閱 反映的安全性考慮。)

若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於

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

來源:
FieldInfo.cs
來源:
FieldInfo.cs
來源:
FieldInfo.cs

當在衍生類別中覆寫時,請設定由指定物件所支援的欄位值。

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.CreateInstanceBinder.ExactBinding )。

binder
Binder

透過反映來啟用繫結、強制引數型別和成員引動過程的一組屬性。 如果 bindernull,則會使用 Binder.DefaultBinding

culture
CultureInfo

特定文化特性 (Culture) 的軟體喜好設定。

實作

例外狀況

呼叫端沒有存取這個欄位的權限。

obj 參數是 null,且欄位為執行個體欄位。

物件上不存在該欄位。

-或-

value 參數無法轉換並儲存在欄位中。

備註

這個方法會指派 value 給 這個實例在 上 obj 反映的欄位。 如果欄位是靜態的, obj 將會忽略。 對於非靜態欄位, obj 應該是繼承或宣告欄位之類別的實例。 新值會傳遞為 Object 。 例如,如果欄位的類型是 Boolean ,則會傳遞具有適當布林值的 實例 Object 。 設定值之前, SetValue 請先檢查使用者是否具有存取權限。

這個方法無法用來可靠地在 C#) 欄位中設定靜態、僅限 init 的 (readonly 值。 在 .NET Core 3.0 和更新版本中,如果您嘗試在僅限靜態的 init 欄位上設定值,就會擲回例外狀況。

注意

完全信任的程式碼具有使用反映來存取及叫用私人建構函式、方法、欄位和屬性所需的許可權。

注意

從 .NET Framework 2.0 Service Pack 1 開始,如果呼叫端已使用 旗標授 ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess 與呼叫端,而且非公用成員的授與集限制為呼叫端的授與集,或子集,則這個方法可用來存取非公用成員。 (請參閱 反映的安全性考慮。)

若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於