public void Initialize(PropertyMapperConfig config) { Config = config; // Create accessor for the column property var columnProp = typeof(IGenericColumns).GetProperty(config.Column); if (columnProp == null || columnProp.PropertyType != typeof(TColumn)) { throw new ArgumentException($"Column not found or type mismatch {config.PropertyName}"); } ColumnAccessor = ReflectionTool.PropertyAccessor <IGenericColumns, TColumn>(columnProp); // Retrieve and validate properties var objectProp = TargetType.GetProperty(config.PropertyName); if (objectProp == null) { throw new ArgumentException($"Target type {TargetType.Name} does not have a property {config.PropertyName}"); } // Create delegates for the object property as well ObjectAccessor = CreatePropertyAccessor(objectProp); }
public void Initialize(Type concreteType, IGenericMapperConfiguration config) { // Get JSON accessor var jsonColumn = typeof(IGenericColumns).GetProperty(config.JsonColumn); _jsonAccessor = ReflectionTool.PropertyAccessor <IGenericColumns, string>(jsonColumn); var baseProperties = typeof(TBase).GetProperties().Select(p => p.Name).ToArray(); var configuredProperties = config.PropertyConfigs.Select(cm => cm.PropertyName); var readOnlyProperties = concreteType.GetProperties() .Where(p => p.GetSetMethod() == null).Select(p => p.Name).ToArray(); // The json should not contain base, configured nor readonly properties var jsonIgnoredProperties = baseProperties .Concat(configuredProperties) .Concat(readOnlyProperties).ToArray(); _jsonSettings = JsonSettings.Minimal .Overwrite(j => j.ContractResolver = new DifferentialContractResolver <TReference>(jsonIgnoredProperties)); // Properties where no mapper should be created for: base and read only properties var mapperIgnoredProperties = baseProperties .Concat(readOnlyProperties).ToArray(); _configuredMappers = config.PropertyConfigs.Where(pc => !mapperIgnoredProperties.Contains(pc.PropertyName)) .Select(pc => MapperFactory.Create(pc, concreteType)).ToArray(); }
public void Initialize(Type concreteType, IGenericMapperConfiguration config) { // Get JSON accessor var jsonColumn = typeof(IGenericColumns).GetProperty(config.JsonColumn); JsonAccessor = ReflectionTool.PropertyAccessor <IGenericColumns, string>(jsonColumn); var baseProperties = typeof(TBase).GetProperties() .Select(p => p.Name); var configuredProperties = config.PropertyConfigs.Select(cm => cm.PropertyName); var ignoredProperties = baseProperties.Concat(configuredProperties).ToArray(); _jsonSettings = JsonSettings.Minimal .Overwrite(j => j.ContractResolver = new DifferentialContractResolver <TReference>(ignoredProperties)); _configuredMappers = config.PropertyConfigs.Select(pc => MapperFactory.Create(pc, concreteType)).ToArray(); }
//[Test] public void PropertyAccessorIsFaster() { // Arrange var childType = typeof(ChildClass1); var child1 = new ChildClass1(); var child2 = new ChildClass1(); var prop = childType.GetProperty(nameof(ChildClass1.Foo)); var accessor = ReflectionTool.PropertyAccessor <BaseClass, int>(prop); // Run once for JIT var value = prop.GetValue(new ChildClass1()); prop.SetValue(new ChildClass1(), 1); accessor.ReadProperty(new ChildClass1()); accessor.WriteProperty(new ChildClass1(), 1); // Act var stopWatch = new Stopwatch(); stopWatch.Start(); for (int i = 0; i < 100; i++) { var next = (int)prop.GetValue(child1) + i; prop.SetValue(child1, next); } stopWatch.Stop(); var reflection = stopWatch.ElapsedTicks; stopWatch.Restart(); for (int i = 0; i < 100; i++) { var next = accessor.ReadProperty(child2) + i; accessor.WriteProperty(child2, next); } stopWatch.Stop(); var accesor = stopWatch.ElapsedTicks; // Asser Assert.AreEqual(child1.Foo, child2.Foo); Assert.Less(accesor, reflection, "Accessor should be faster"); }
public void PropertyAccessor() { // Arrange var childType = typeof(ChildClass1); var prop = childType.GetProperty(nameof(ChildClass1.Foo)); // Act var accessor0 = ReflectionTool.PropertyAccessor(prop); var child0 = new ChildClass1(); accessor0.WriteProperty(child0, 42); var accessor1 = ReflectionTool.PropertyAccessor <IBaseInterface>(prop); var child1 = new ChildClass1(); accessor1.WriteProperty(child1, 42); var child2 = new ChildClass1(); accessor1.WriteProperty(child2, 42L); var accessor2 = ReflectionTool.PropertyAccessor <BaseClass, long>(prop); var child3 = new ChildClass1(); accessor2.WriteProperty(child3, 42L); var accessor3 = ReflectionTool.PropertyAccessor <BaseClass, short>(prop); var child4 = new ChildClass1(); accessor3.WriteProperty(child4, 42); var accessor4 = ReflectionTool.PropertyAccessor <ChildClass1, int>(prop); var child5 = new ChildClass1(); accessor4.WriteProperty(child5, 42); // Assert Assert.AreEqual(42, child0.Foo); Assert.AreEqual(42, child1.Foo); Assert.AreEqual(42, child2.Foo); Assert.AreEqual(42, child3.Foo); Assert.AreEqual(42, child4.Foo); Assert.AreEqual(42, child5.Foo); }
protected ConversionAccessor(PropertyInfo property) { Target = ReflectionTool.PropertyAccessor <object, TProperty>(property); }
protected virtual IPropertyAccessor <object, TColumn> CreatePropertyAccessor(PropertyInfo objectProp) { // Default conversions return(ReflectionTool.PropertyAccessor <object, TColumn>(objectProp)); }
public ConversionAccessor(PropertyInfo property, Func <TProperty, TColumn> toColumn, Func <TColumn, TProperty> toProperty) { ToColumn = toColumn; ToProperty = toProperty; Target = ReflectionTool.PropertyAccessor <object, TProperty>(property); }