C#: Quickly Propagating Properties of an Object to the Parent Class

To quickly implement the propagation of all of the methods from an object owned by a class (called “theObj” below), to the owner, you can use the C# Interactive Window. Right-click your C# project, and choose “Initialize Interactive with Project”. Then, create an instance of your object, and extract the methods in the interactive window using the code below. Copy and paste the methods into your class’s code, and then manually edit any problematic results (i.e. generics won’t appear properly).

You can repeat this process for properties and events, simply changing the code to suit your needs.

theObj = new TheObjectTypeIWantToExpose();
var props = theObj.GetType().GetMethods(System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

foreach (var prop in props) {
    if (prop.Name.StartsWith("get_") || prop.Name.StartsWith("set_") ||
        prop.Name.StartsWith("add_") || prop.Name.StartsWith("remove_")) continue;

    var parameters = prop.GetParameters();
    List<string> paramlist = new List<string>();
    List<string> paramnames = new List<string>();
    foreach(var param in parameters)
        string curParam = param.ParameterType.FullName + " " + param.Name;

    string allParams = string.Join(",", paramlist);
    string allNames = string.Join(",", paramnames);

    Console.WriteLine($"{prop.ReturnType.FullName} {prop.Name}({allParams}) => TheChildObject.{prop.Name}({allNames});");