Logging Experiences

Assigning values to private fields using reflection

Posted in C#, Programming by Sina Iravanian on March 6, 2010

Suppose that you have a class with various non-public fields, e.g., the class named ClassToTest below servers as a good example.

public class ClassToTest
    public int SomeNumber { get; set; }

    private int m_somePrivateProperty { get; set; }
    private int m_somePrivateVar;

    private static int s_someStaticNumber;

And suppose that we desire to assign some value to the private variable m_somePrivateVar, and the private property m_somePrivateProperty. The approach is to iterate through the fields of the ClassToTest type to find the desired field. If it is a variable we should type-cast it to FieldInfo, and if it is a property we should type-cast it to PropertyInfo. Then we should call the SetValue method appropriately. This is shown below:

ClassToTest c = new ClassToTest();

Type t = typeof(ClassToTest);
foreach (var member in t.GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
    if (member.Name == "m_somePrivateVar")
        // changing the variable
        FieldInfo fi = (FieldInfo)member;
        fi.SetValue(c, 18);
    else if (member.Name == "m_somePrivateProperty")
        // changing the property
        PropertyInfo pi = (PropertyInfo)member;
        pi.SetValue(c, 13, null);

Here we have successfully assigned values to selected private fields of our class.

Tagged with: ,

One Response

Subscribe to comments with RSS.

  1. farhad said, on July 7, 2010 at 1:41 pm

    this is such a great code.. just let me learn it for a while.. đŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: