SerializationInfo extension method for typifing GetValue

This is a handy extension method I recurrently use to help me with the deserialization process. It helps to keep the code clean on the serialization constructor by strongly typing the GetValue method on the SerializationInfo class.

/// <summary>
/// Static class that contains extensions to <seealso cref="SerializationInfo"/>.
/// </summary>
public static class SerializationInfoExtensions
{
    /// <summary>
    /// Retrieves a value from the SerializationInfo store.
    /// </summary>
    /// <typeparam name="T">The type of the value to retrieve.</typeparam>
    /// <param name="info">The SerializationInfo instance.</param>
    /// <param name="name">The name of the parameter to retrieve.</param>
    /// <returns>The deserialized instance of T.</returns>
    public static T GetValue<T>(this SerializationInfo info, string name)
    {
        return (T) info.GetValue(name, typeof(T));
    }
}

When this extension method is in scope we can easily retrieve strongly typed data from SerializationInfo:

appConfiguration = info.GetValue<ApplicationConfiguration>("AppConfiguration");
appPreferences = info.GetValue<AppPreferences>("AppPreferences");

This is just a small sample on how helpful and powerful extension methods can be!

Setting a Private Field on an instance of an object using Reflection

For sure this is not a best practice, although I thought of using this in a hacky way to inject a value into a private field of a class, I ended up not using this code anywhere. It is amazing the freedom you get when you use reflection, you can do just about anything!

Let’s start by checking the Person class, which contains a private string for name:

public class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        private set { name = value; }
    }

    public Person(string name)
    {
        this.name = name;
    }
}

Since the set for the Name property as a private access modifier we will not be able to set a Person’s name the normal way. As we want to access the private field name from outside of the class, let us create an helper method that does just that, and generalize it so that we can use it anywhere:

public static void SetField<T>(T instance, string fieldName, object obj)
{
    var f = typeof(T)
        .GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
    if (obj.GetType() != f.FieldType)
        throw new InvalidOperationException("Types do not match!");
    f.SetValue(instance, obj);
}

Supposing we have a the SetField method in scope, we can call it passing an instance of an object, the name of the field (private or not) to be set and the object to set it to. Using the previous Person class we can easily call:

SetField(person, "name", "Jason");

Et voilá! The name of person gets set to Jason, even though the access modifier on the field is private.

If you want to download a full working sample for this example please follow this link.