Object - SetPropertyValue

A T extension method that sets property value.

Try it

public static void Main()
{
	var obj = new TestClass();

	//C# Extension Method: Object - SetPropertyValue	
    obj.SetPropertyValue("PublicField", 5);
    obj.SetPropertyValue("InternaStaticlField", 30);
	
	Console.WriteLine(obj.PublicField);
	Console.WriteLine(TestClass.InternaStaticlField);
}

public class TestClass
{
    internal static int InternaStaticlField { get; set; }
    public int PublicField { get; set; }
}

View Source
using System;
using System.Reflection;

public static partial class Extensions
{
    /// <summary>
    ///     A T extension method that sets property value.
    /// </summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <param name="propertyName">Name of the property.</param>
    /// <param name="value">The value.</param>
    public static void SetPropertyValue<T>(this T @this, string propertyName, object value)
    {
        Type type = @this.GetType();
        PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
        property.SetValue(@this, value, null);
    }
}