public static void Execute() { Stopwatch stopWatch = Stopwatch.StartNew(); BaseObject person = DynamicClassInitializer.CreateObjectInstanceByName("Person"); BaseObject address = DynamicClassInitializer.CreateObjectInstanceByName("Address"); Type personType = person.GetType(); var firstNameProperty = personType.GetProperty("FirstName"); var lastNameProperty = personType.GetProperty("LastName"); var addressProperty = personType.GetProperty("Address"); Type addressType = address.GetType(); var address1Property = addressType.GetProperty("Address1"); var postalCodeProperty = addressType.GetProperty("PostalCode"); for (int i = 0; i < Program.Iterations; i++) { // use reflected property information to set values on the instance firstNameProperty.SetValue(person, "John"); lastNameProperty.SetValue(person, "Smith"); address1Property.SetValue(address, "1234 Main St"); postalCodeProperty.SetValue(address, "12345"); addressProperty.SetValue(person, address); } stopWatch.Stop(); Console.WriteLine("Example 5a setting properties through reflection with reflection outside loop Elapsed time {0} ms", stopWatch.ElapsedMilliseconds); }
public static void Execute() { Stopwatch stopWatch = Stopwatch.StartNew(); for (int i = 0; i < Program.Iterations; i++) { BaseObject address = DynamicClassInitializer.CreateObjectInstanceByName("Address"); // create a new Person BaseObject person = DynamicClassInitializer.CreateObjectInstanceByName("Person"); } stopWatch.Stop(); Console.WriteLine("Example 2 Activator.CreateInstance Instantiation Elapsed time {0} ms", stopWatch.ElapsedMilliseconds); }
public static void Execute() { Stopwatch stopWatch = Stopwatch.StartNew(); BaseObject person = DynamicClassInitializer.CreateObjectInstanceByName("Person"); BaseObject address = DynamicClassInitializer.CreateObjectInstanceByName("Address"); for (int i = 0; i < Program.Iterations; i++) { // use compiled and cached actions to set values for each property DynamicClassInitializer.SetPropertyForObject("Person", "FirstName", person, "John"); DynamicClassInitializer.SetPropertyForObject("Person", "LastName", person, "Smith"); DynamicClassInitializer.SetPropertyForObject("Address", "Address1", address, "1234 Main St"); DynamicClassInitializer.SetPropertyForObject("Address", "PostalCode", address, "12345"); DynamicClassInitializer.SetPropertyForObject("Person", "Address", person, address); } stopWatch.Stop(); Console.WriteLine("Example 6 setting properties using cached lambdas for setters Elapsed time {0} ms", stopWatch.ElapsedMilliseconds); }