Freigeben über


FieldInfo.SetValue Method (Object, Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets the value of the field that is supported by the given object.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<DebuggerStepThroughAttribute> _
<DebuggerHiddenAttribute> _
Public Sub SetValue ( _
    obj As Object, _
    value As Object _
)
[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public void SetValue(
    Object obj,
    Object value
)

Parameters

  • obj
    Type: System.Object
    The object whose field value will be set.

Exceptions

Exception Condition
FieldAccessException

The field is not accessible.

TargetException

The obj parameter is nulla null reference (Nothing in Visual Basic) and the field is an instance field.

ArgumentException

The field does not exist on the object.

-or-

The value parameter cannot be converted and stored in the field.

MethodAccessException

The member is invoked late-bound through mechanisms such as Type.InvokeMember.

Remarks

In Silverlight, only accessible fields can be set using reflection.

This method will assign value to the field reflected by this instance on object obj. If the field is static, obj will be ignored. For nonstatic fields, obj should be an instance of a class that inherits or declares the field. The new value is passed as an Object. For example, if the field is of type Boolean, the new value must be boxed as an instance of Object.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 SetValue throws an ArgumentNullException exception instead of a TargetException exception when obj is nulla null reference (Nothing in Visual Basic).

If you call the SetValue method on a field that is defined as both public and constant, a MemberAccessException exception is thrown instead of a FieldAccessException exception.

Examples

The following example gets the value of each field of the Example class, modifies the value by appending a string to it, and sets the new value. It then retrieves the new value and displays the result. If a field value cannot be retrieved because of the field's access level, the exception is caught and a message is displayed. The internal fields (Friend fields in Visual Basic) are accessible in this example because the Example and Test classes are in the same assembly.


Imports System.Reflection

Public Class Test

    Public Shared SA As String = "A public shared field."
    Friend Shared SB As String = "A friend shared field."
    Protected Shared SC As String = "A protected shared field."
    Private Shared SD As String = "A private shared field."

    Public A As String = "A public instance field."
    Friend B As String = "A friend instance field."
    Protected C As String = "A protected instance field."
    Private D As String = "A private instance field."

End Class

Public Class Example

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        ' Create an instance of Test, and get a Type object.
        Dim myInstance As New Test()
        Dim t As Type = GetType(Test)

        ' Get the shared fields of Test, and change their values. This does not 
        ' require an instance of Test, so Nothing is passed to SetValue.
        Dim sharedFields As FieldInfo() = _
            t.GetFields(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static)
        For Each f As FieldInfo In sharedFields
            Try
                ' Append a marker to the old value.
                Dim newValue As String = f.GetValue(Nothing) & " -modified-"

                f.SetValue(Nothing, newValue)

                outputBlock.Text += _
                    String.Format("The value of Shared field {0} is: {1}" & vbLf, _
                                  f.Name, _
                                  f.GetValue(Nothing))
            Catch 
                outputBlock.Text += _
                    String.Format("The value of Shared field {0} is not accessible." & vbLf, _
                                  f.Name)
            End Try
        Next f

        ' Get the instance fields of Test, and change their values for the instance 
        ' created earlier.
        Dim instanceFields As FieldInfo() = _
            t.GetFields(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance)
        For Each f As FieldInfo In instanceFields
            Try
                ' Append a marker to the old value.
                Dim newValue As String = f.GetValue(myInstance) & " -modified-"

                f.SetValue(myInstance, newValue)

                outputBlock.Text += _
                    String.Format("The value of instance field {0} is: {1}" & vbLf, _
                                  f.Name, _
                                  f.GetValue(myInstance))
            Catch 
                outputBlock.Text += _
                    String.Format("The value of instance field {0} is not accessible." & vbLf, _
                                  f.Name)
            End Try
        Next f

   End Sub

End Class 

' This example produces the following output:
'
'The value of Shared field SA is: A public shared field. -modified-
'The value of Shared field SB is: A friend shared field. -modified-
'The value of Shared field SC is not accessible.
'The value of Shared field SD is not accessible.
'The value of instance field A is: A public instance field. -modified-
'The value of instance field B is: A friend instance field. -modified-
'The value of instance field C is not accessible.
'The value of instance field D is not accessible.
using System;
using System.Reflection;

public class Test
{
    public static string SA = "A public shared field.";
    internal static string SB = "A friend shared field.";
    protected static string SC = "A protected shared field.";
    private static string SD = "A private shared field.";

    public string A = "A public instance field.";
    internal string B = "A friend instance field.";
    protected string C = "A protected instance field.";
    private string D = "A private instance field.";
}

public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        // Create an instance of Test, and get a Type object.
        Test myInstance = new Test();
        Type t = typeof(Test);

        // Get the shared fields of Test, and change their values. This does not 
        // require an instance of Test, so Nothing is passed to SetValue.
        FieldInfo[] sharedFields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | 
                                               BindingFlags.Static);
        foreach( FieldInfo f in sharedFields )
        {
            try
            {
                // Append a marker to the old value.
                string newValue = f.GetValue(null) + " -modified-";

                f.SetValue(null, newValue);

                outputBlock.Text += String.Format("The value of Shared field {0} is: {1}\n", 
                                                  f.Name, 
                                                  f.GetValue(null));
            }
            catch
            {
                outputBlock.Text += 
                   String.Format("The value of Shared field {0} is not accessible.\n", f.Name);
            }
        }

        // Get the instance fields of Test, and change their values for the instance 
        // created earlier.
        FieldInfo[] instanceFields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | 
                                                 BindingFlags.Instance);
        foreach( FieldInfo f in instanceFields )
        {
            try
            {
                // Append a marker to the old value.
                string newValue = f.GetValue(myInstance) + " -modified-";

                f.SetValue(myInstance, newValue);

                outputBlock.Text += String.Format("The value of instance field {0} is: {1}\n", 
                                                  f.Name, 
                                                  f.GetValue(myInstance));
            }
            catch
            {
                outputBlock.Text += 
                   String.Format("The value of instance field {0} is not accessible.\n", f.Name);
            }
        }
    }
}

/* This example produces the following output:

The value of Shared field SA is: A public shared field. -modified-
The value of Shared field SB is: A friend shared field. -modified-
The value of Shared field SC is not accessible.
The value of Shared field SD is not accessible.
The value of instance field A is: A public instance field. -modified-
The value of instance field B is: A friend instance field. -modified-
The value of instance field C is not accessible.
The value of instance field D is not accessible.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.