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 void InitializeDynamicClasses(List <DynamicClass> dynamicClasses) { if (DynamicClassInitializer.IsInitialized) { return; } DynamicClassInitializer.InitializeDynamicClasses(dynamicClasses); }
public static void Execute() { Stopwatch stopWatch = Stopwatch.StartNew(); for (int i = 0; i < Program.Iterations; i++) { BaseObject address = DynamicClassInitializer.CreateObjectInstanceByTypeNameLambda("Address"); // create a new Person BaseObject person = DynamicClassInitializer.CreateObjectInstanceByTypeNameLambda("Person"); } stopWatch.Stop(); Console.WriteLine("Example 3a Lambda Instantiation Cache By Type 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); }
static void Main(string[] args) { DynamicClass addressCLass = new DynamicClass() { ObjectName = "Address", Fields = new List<Field>() { new Field() { PropertyName = "Address1", PropertyType = typeof(string) }, new Field() { PropertyName = "Address2", PropertyType = typeof(string) }, new Field() { PropertyName = "PostalCode", PropertyType = typeof(string) } } }; DynamicClass dynamicClass = new DynamicClass() { ObjectName = "Person", Fields = new List<Field>() { new Field() { PropertyName = "FirstName", PropertyType = typeof(string) }, new Field() { PropertyName = "LastName", PropertyType = typeof(string) }, new Field() { PropertyName = "Age", PropertyType = typeof(int) }, new Field() { PropertyName = "Address", DynamicRuntimeType = "Address" } } }; DynamicClassInitializer.InitializeDynamicClasses(new List<DynamicClass>() { addressCLass, dynamicClass }); Example1.Execute(); Example2.Execute(); Example3.Execute(); Example3A.Execute(); Example4.Execute(); Example5.Execute(); Example5A.Execute(); Example6.Execute(); }