Object - ShallowCopy

A T extension method that shallow copy.

Try it

public static void Main()
{
	var Obj = new TestClass {Value = "Fizz"};

	// C# Extension Method: Object - ShallowCopy
    TestClass clone = Obj.ShallowCopy();
	Console.WriteLine("{0} = {1} : {2}", Obj.Value, clone.Value, Obj.Value == clone.Value);
	
}

[Serializable]
public class TestClass
{
    public string Value;
}

View Source
using System.Reflection;

public static partial class Extensions
{
    /// <summary>
    ///     A T extension method that shallow copy.
    /// </summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <returns>A T.</returns>
    public static T ShallowCopy<T>(this T @this)
    {
        MethodInfo method = @this.GetType().GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
        return (T) method.Invoke(@this, null);
    }
}