public override MapResult Map <T>(ValueMapperContext context) { if (typeof(T) != typeof(int)) { return(MapResult.Success(default(T))); } var result = context.Values.SelectMany(x => x.ToString().Split(',')) .Select(x => (T)(object)int.Parse(x.ToString())); return(MapResult.Success(context.Type.IsArray ? result.ToArray() : (context.Type.IsGenericListCastable ? result.ToList() : (object)result.FirstOrDefault()))); }
public void Should_set_properties_faster_than_reflection() { var mapper = new TestValueMapper(_configuration); var mapperContext = new ValueMapperContext(new ActionParameter(_actionMethod, Properties[nameof(Model.Value)]), new object[] { "1,2,3" }); var method = typeof(TestValueMapper) .GetMethods(BindingFlags.Public | BindingFlags.Instance) .First(x => x.Name == nameof(TestValueMapper.Map) && x.IsGenericMethod) .MakeGenericMethod(mapperContext.Type.Type); var comparison = PerformanceComparison.InTicks(10000, 1000); comparison.AddCase("Native", () => mapper.Map <int>(mapperContext)); var compiledCase = comparison.AddCase("Compiled expression", () => mapper.Map(mapperContext)); var reflectionCase = comparison.AddCase("Reflection", () => method.Invoke(mapper, new object[] { mapperContext })); comparison.Run(); compiledCase.Average.ShouldBeLessThan(reflectionCase.Average); }
public void Should_set_properties_faster_than_reflection() { var iterations = 10000; var mapper = new TestValueMapper(); var mapperContext = new ValueMapperContext(null, null, Properties[nameof(Model.Value)], new object[] { "1,2,3" }); var method = typeof(TestValueMapper) .GetMethods(BindingFlags.Public | BindingFlags.Instance) .First(x => x.Name == nameof(TestValueMapper.Map) && x.IsGenericMethod) .MakeGenericMethod(mapperContext.Type.Type); var nativeElapsed = new List <long>(); var reflectionElapsed = new List <long>(); var expressionElapsed = new List <long>(); Action run = () => { expressionElapsed.Add(mapper.ElapsedTicks(x => x.Map(mapperContext))); reflectionElapsed.Add(method.ElapsedTicks(x => { x.Invoke(mapper, new object[] { mapperContext }); })); nativeElapsed.Add(mapper.ElapsedTicks(x => { x.Map <int>(mapperContext); })); }; 100.Times(run); // Warmup nativeElapsed.Clear(); reflectionElapsed.Clear(); expressionElapsed.Clear(); iterations.Times(run); Console.WriteLine($"Native: {nativeElapsed.Average()}"); Console.WriteLine($"Compiled expression: {expressionElapsed.Average()}"); Console.WriteLine($"Reflection: {reflectionElapsed.Average()}"); }
public object Map(ValueMapperContext context) { MapCalled = true; MapContext = context; return(MapFunc(context)); }
public bool AppliesTo(ValueMapperContext context) { AppliesToCalled = true; AppliesToContext = context; return(AppliesToFunc?.Invoke(context) ?? true); }
public override bool AppliesTo(ValueMapperContext context) { throw new NotImplementedException(); }