I wrote that as a quick hack to solve a specific, temporary problem a few weeks ago and I knew it wasn't going to be very efficient, but it didn't matter in that instance. The best solutions in this sort of problem are MSIL assuming you cannot just change the classes to share dependencies, but MSIL is not something very many folks can read, and even fewer can write.
In .Net 4, you get some interesting additions/better documentation to Linq show you how to build expressions on the fly, so I wanted to take a stab at re-writing the converter that way.
The general idea is the same, use reflection to look for fields and writable properties then generate some action to sync them up, but there are several differences:
- reflection is only used the first time a pair of types is converted
- the actions are generated as Linq that is then compiled before storing in the converters set
- added the ability to set a property to a field or field to property
- added type checking
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace typeoverridedemostrater { static class SuperConverter<T1, T2> where T2 : new() { private static Dictionary<Tuple<System.Type, System.Type>, Func<T1, T2>> converters = new Dictionary<Tuple<System.Type, System.Type>, Func<T1, T2>>(); static public T2 Convert(T1 i) { var key = new Tuple<System.Type, System.Type>(typeof(T2), typeof(T1)); if (!converters.ContainsKey(key)) { ParameterExpression value = Expression.Parameter(typeof(T1), "value"); ParameterExpression result = Expression.Parameter(typeof(T2), "result"); var exprs = new List<Expression>(); exprs.Add(Expression.Assign(result, Expression.New(typeof(T2)))); exprs.AddRange(( from z in typeof(T2).GetProperties().Where(a => a.CanWrite) .Select(b => new { name = b.Name, type = b.PropertyType }).Union( typeof(T2).GetFields().Select(c => new { name = c.Name, type = c.FieldType })) join y in typeof(T1).GetProperties().Where(a => a.CanWrite) .Select(b => new { name = b.Name, type = b.PropertyType }).Union( typeof(T1).GetFields().Select(c => new { name = c.Name, type = c.FieldType })) on z equals y select Expression.Assign(Expression.PropertyOrField(result, z.name), Expression.PropertyOrField(value, z.name)) ).ToArray() ); exprs.Add(result); BlockExpression block = Expression.Block(variables: new[] { result }, expressions: exprs.ToArray()); converters.Add(key, Expression.Lambda<Func<T1, T2>>(block, value).Compile()); } return converters[key].Invoke(i); } } }
the general flow is:
- if you don't already have a good converter
- create an input value and output result
- create an action to assign a new instance to the result variable
- add an assign action for every param/field that has a match between the types
- add an action for returning the result
- turn those into an expression block
- turn the block Lambda, then compiled to a TDelegate and store in the converters dictonary
- run the TDelegate from the dictionary
For 100,000 conversions the before/after is:
TotalMilliseconds = 1609.4059 vs TotalMilliseconds = 93.7518
No comments:
Post a Comment