public virtual void Set <T, TResult>(Func <T, TResult> conversionMethod, string scenario = null) { var key = new ConvertionKey(scenario, typeof(T), typeof(TResult)); Func <object, object> objMutator = obj => conversionMethod((T)obj); _mutators.AddOrUpdate(key, (ConvertionKey k) => objMutator, (k, oldValue) => objMutator); }
public virtual Func <object, object> Get(Type sourceType, Type targetType, string scenario = null) { if (sourceType.IsByRef) { sourceType = sourceType.GetElementType(); } if (targetType.IsByRef) { targetType = sourceType.GetElementType(); } if (sourceType == targetType) { return(new Func <object, object>(JustReturn)); } if (sourceType == typeof(void)) { return(new Func <object, object>(JustReturn)); } Func <object, object> mutator; var key = new ConvertionKey(scenario, sourceType, targetType); if (_mutators.TryGetValue(key, out mutator)) { return(mutator); } // Fail back to default if not find. key = new ConvertionKey(null, sourceType, targetType); if (_mutators.TryGetValue(key, out mutator)) { return(mutator); } throw new InvalidOperationException(string.Format("The mutation function from {0} to {1} has not been set yet.", sourceType, targetType)); }