- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
public class Switch
{
public Switch(object switchElement)
{
Object = switchElement;
}
public object Object{get; private set; }
}
public static class SwitchExtensions
{
public static Switch Case<T>(this Switch switchCase, Func<T, bool> isTrue, Action<T> action) where T : class
{
return Case(switchCase, isTrue, action, false);
}
public static Switch Case<T>(this Switch switchCase, Func<T, bool> isTrue, Action<T> action, bool fallThrough) where T : class
{
if (switchCase == null)
return null;
var obj = switchCase.Object as T;
if (obj != null)
if (isTrue(obj))
{
action(obj);
return fallThrough ? switchCase : null;
}
return switchCase;
}
}
//пример использования
IImporter<AccountModel> result = null;
new Switch(type)
.Case<string>(x => x == ".xls", y => result = new ExcelImporter())
.Case<string>(x => x == ".csv", y => result = new CsvImporter());
return result;
У нас будет свой Switch/Case с преферансом, куртизанками и экстеншен методами