private object ConvertImpl(object value, Type targetType, SwitchConvertDirection direction) { var valueType = value?.GetType() ?? typeof(object); Default <TKey, TValue> defaultCase = null; foreach (var option in Options) { if (option is Case <TKey, TValue> caseOption) { var caseKey = GetKey(caseOption, direction, valueType); if (Equals(value, caseKey)) { return(GetValue(caseOption, direction, targetType)); } continue; } var defaultOption = option as Default <TKey, TValue>; if (defaultOption != null && defaultCase != null) { throw new Exception("SwitchConverter can have only 1 default case"); } defaultCase = defaultOption; } return(defaultCase != null?GetValue(defaultCase, direction, targetType) : FallBackCase.Value.XamlConvert(targetType)); }
private static object GetValue(SwitchOption <TKey, TValue> switchOption, SwitchConvertDirection direction, Type targetType) { return(direction == SwitchConvertDirection.Direct ? (object)switchOption.XamlConvertValue(targetType) : switchOption.XamlConvertKey(targetType)); }