private void Emit(List <KeyMock> keys) { var mainKey = keys.Where(key => key.KeyModifier == KeyModifier.None).Last(); var keyModifier = keys .Where(key => key.KeyModifier != KeyModifier.None) .Select(key => key.KeyModifier) .Aggregate((k1, k2) => k1 | k2); var shortcut = new ShortcutMock(mainKey, keyModifier); var name = mainKey.Name; if (RegisteredShortcuts.ContainsKey(name)) { var registeredShortcutsForKey = RegisteredShortcuts[name]; if (registeredShortcutsForKey != null) { if (registeredShortcutsForKey.ContainsKey(keyModifier)) { var registeredShortcut = registeredShortcutsForKey[keyModifier]; if (registeredShortcut != null) { shortcut = registeredShortcut.Shortcut; if (registeredShortcut.Operation != null) { registeredShortcut.Operation.Action(); } } } } } OnEmit?.Invoke(shortcut); }
/// <summary> /// Contructs immutable object from existing one with changed property specified by lambda expression. /// </summary> /// <typeparam name="TInstance">Immutable object type.</typeparam> /// <typeparam name="TValue">Value to set type.</typeparam> /// <param name="source">Original immutable object.</param> /// <param name="expression">Navigation property specifying what to change.</param> /// <param name="value">Value to set in the resulting object.</param> /// <returns>Immutable object with changed property.</returns> public TInstance With <TInstance, TValue>(TInstance source, Expression <Func <TInstance, TValue> > expression, TValue value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (expression == null) { throw new ArgumentNullException(nameof(expression)); } var processContext = new ProcessContext <TInstance>() { Source = source, Target = value as object, SourceParameterExpression = expression.Parameters.Single(), InstanceExpression = expression.Body }; var actualValue = (TValue)ResolveInstance(processContext); if (object.Equals(actualValue, value)) { return(source); } while (processContext.InstanceExpression != processContext.SourceParameterExpression) { if (TryProcessMemberExpression(processContext)) { continue; } throw new NotSupportedException($"Unable to process expression. Expression: '{processContext.InstanceExpression}'."); } var target = (TInstance)processContext.Target; processContext.AffectedProperties.Reverse(); OnEmit?.Invoke(source, target, value, processContext.AffectedProperties.ToArray()); return(target); }
/// <summary> /// Constructs immutable object from any other object. /// Helpful cloning immutable object or converting POCO, DTO, anonymous type, dynamic ect. /// </summary> /// <typeparam name="TInstance">Immutable object type.</typeparam> /// <param name="source">Original object.</param> /// <returns>Immutable object.</returns> public TInstance With <TInstance>(object source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceType = source.GetType(); var targetType = typeof(TInstance); var activationContext = GetActivationContext(sourceType, targetType); var result = default(object); var arguments = ResolveActivatorArguments(activationContext.ParameterResolvers, null, source, ref result); result = activationContext.Activator.Invoke(arguments); var target = (TInstance)result; OnEmit?.Invoke(source, target, null, null); return(target); }