共用方式為


FieldInfo.SetValue 方法

定義

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

多載

名稱 Description
SetValue(Object, Object)

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

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

當在派生類別中被覆寫時,會設定該物件所支援欄位的值。

SetValue(Object, Object)

來源:
FieldInfo.cs
來源:
FieldInfo.cs
來源:
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

要指派給欄位的值。

實作

例外狀況

來電者無權存取此欄位。

注意:在 .NET for Windows Store 應用程式Portable 類別函式庫中,請擷取基底類別例外, MemberAccessException為 。

參數 obj 為 , null 欄位為實例欄位。

注意:在 Windows 商店 .NET 應用程式Portable 類別庫中,請選擇 catch Exception

該場不存在於物體上。

-或-

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 方法。

此方法無法可靠地設定靜態、僅初始化(readonly C#)欄位的值。 在 .NET 版本中,若嘗試在靜態且僅 init 欄位設定值,會拋出例外。

備註

完全受信任的程式碼擁有使用反射存取及調用私有建構子、方法、欄位與屬性所需的權限。

備註

若呼叫者已獲得ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess該旗標,且非公開成員的授權集限制於呼叫者的授權集或其子集,此方法可用來存取非公開成員。 (詳見 安全考量以資反思。)

要使用此功能,您的應用程式應針對 .NET Framework 3.5 或更新版本。

適用於

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

來源:
FieldInfo.cs
來源:
FieldInfo.cs
來源:
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

一組性質使參數類型能夠約束、強制,並透過反射調用成員。 若 bindernullBinder.DefaultBinding 使用 。

culture
CultureInfo

特定文化的軟體偏好。

實作

例外狀況

來電者無權存取此欄位。

參數 obj 為 , null 欄位為實例欄位。

該場不存在於物體上。

-或-

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

備註

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

此方法無法可靠地設定靜態、僅初始化(readonly C#)欄位的值。 在 .NET 中,如果你嘗試在靜態且僅初始化的欄位上設定值,會拋出例外。

備註

完全受信任的程式碼擁有使用反射存取及調用私有建構子、方法、欄位與屬性所需的權限。

備註

若呼叫者已獲得ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess該旗標,且非公開成員的授權集限制於呼叫者的授權集或其子集,此方法可用來存取非公開成員。 (詳見 安全考量以資反思。)

適用於