using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace LateBoundCalls { class Program { static void Main(string[] args) { DemoTraditionalReflection(); DemoDynamicTyping(); Console.ReadLine(); } private static void DemoTraditionalReflection() { Console.WriteLine("Using tradional reflection"); Console.WriteLine("=========================="); Assembly asm = Assembly.Load("MyUtilities"); try { // Get metadata for the MyMathUtilities type. Type math = asm.GetType("MyUtilities.MyMathUtilities"); // Create a MyMathUtilities object on the fly. object obj = Activator.CreateInstance(math); // Get info for Add. MethodInfo mi = math.GetMethod("Add"); // Invoke method (with parameters). object[] args = { 10, 70 }; Console.WriteLine("Result is: {0}", mi.Invoke(obj, args)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } private static void DemoDynamicTyping() { Console.WriteLine("\nUsing dynamic typing"); Console.WriteLine("===================="); Assembly asm = Assembly.Load("MyUtilities"); try { // Get metadata for the MyMathUtilities type. Type math = asm.GetType("MyUtilities.MyMathUtilities"); // Create a MyMathUtilities object on the fly. dynamic obj = Activator.CreateInstance(math); // Invoke method (with parameters). Console.WriteLine("Result is: {0}", obj.Add(10, 70)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }