Field value

In this chapter you will learn:

  1. How to use reflection to get field value

Get field value

FieldInfo.GetValue nethod returns the value of a field supported by a given object.

using System;//from ja va2s .  c o m
using System.Reflection;

class MyClass
{
    public static String val = "test";
    public static void Main()
    {
        FieldInfo myf = typeof(MyClass).GetField("val");
        Console.WriteLine(myf.GetValue(null));
        val = "hi";
        Console.WriteLine(myf.GetValue(null));
    }
}

Next chapter...

What you will learn in the next chapter:

  1. Get/set private field using InvokeMember
Home » C# Tutorial » Reflection
Reflection
Type
Type properties
Field reflection
Field Type
Field Attributes
FieldHandle
Field value
Set Field value
delegate reflection
Event reflection
Indexer reflection
Properties Reflection
Method
Parameter
Invoke
Type Instantiating
interface reflection
Generic type reflection
Reflection on nested Types
Subtype
Array reflection
Assembly