FieldInfo.GetValue Method

When overridden in a derived class, returns the value of a field supported by a given object.

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

'Declaration
Public MustOverride Function GetValue ( _
	obj As Object _
) As Object

Parameters

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

Return Value

Type: System.Object
An object that contains the value of the field reflected by this instance.

ExceptionCondition
TargetException

The field is nonstatic and obj is Nothing.

NotSupportedException

A field is marked literal, but the field does not have one of the accepted literal types.

FieldAccessException

The field is not accessible.

ArgumentException

The method is neither declared nor inherited by the class of obj.

MethodAccessException

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

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

If the field is static, obj is ignored. For nonstatic fields, obj should be an instance of a class that inherits or declares the field. Note that the return type of GetValue is Object. For example, if the field is of type Boolean, its value is boxed and returned as an instance of Object.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetValue throws an ArgumentNullException when null is passed as first parameter.

The following example retrieves all the fields of the Test class and displays the field values. 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 display their values. This does not 
        ' require an instance of Test, so Nothing is passed to GetValue.
        Dim sharedFields As FieldInfo() = _
            t.GetFields(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static)
        For Each f As FieldInfo In sharedFields
            Try
                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 display 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
                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.
'The value of Shared field SB is: A friend shared field.
'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.
'The value of instance field B is: A friend instance field.
'The value of instance field C is not accessible.
'The value of instance field D is not accessible.


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

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

Community Additions

ADD
Show: