/// <summary> /// The <see cref="Main"/> method demonstrates the runtime creation of /// an adapter class that adapts the <see cref="Target"/> class to the /// <see cref="IAddNumbers"/> interface. /// </summary> /// <param name="args">Not used.</param> static void Main(string[] args) { // This is the instance of the Target that we'll adapt to the // IAddNumbers interface. Note that it does not implement the // IAddNumbers interface. Target target = new Target(); // The AdapterBuilder.Build method does all that magic of // creating a class using reflection and IL emission. It then // instantiates that class, sets target onto a field of the // object, and returns it as an IAddNumbers. IAddNumbers adder = AdapterBuilder.Build<IAddNumbers, Target>(target); // Use the newly created instance of the generated-at-runtime class // that will forward the method call to the instance of Target // created on line 44. int result = adder.Add(5, 6); // This prints out the result. Console.WriteLine("The result of adding 5 and 6: {0}", result); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(true); }