|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| - |
5 |
| -namespace GenHTTP.Modules.Conversion.Formatters |
6 |
| -{ |
7 |
| - |
8 |
| - public sealed class FormatterRegistry |
9 |
| - { |
10 |
| - |
11 |
| - #region Get-/Setters |
12 |
| - |
13 |
| - public IReadOnlyList<IFormatter> Formatters { get; private set; } |
14 |
| - |
15 |
| - #endregion |
16 |
| - |
17 |
| - #region Initialization |
18 |
| - |
19 |
| - public FormatterRegistry(List<IFormatter> formatters) |
20 |
| - { |
21 |
| - Formatters = formatters; |
22 |
| - } |
23 |
| - |
24 |
| - #endregion |
25 |
| - |
26 |
| - #region Functionality |
27 |
| - |
28 |
| - public bool CanHandle(Type type) => Formatters.Any(f => f.CanHandle(type)); |
29 |
| - |
30 |
| - public object? Read(string value, Type type) => Formatters.First(f => f.CanHandle(type)).Read(value, type); |
31 |
| - |
32 |
| - public string? Write(object value, Type type) => Formatters.First(f => f.CanHandle(type)).Write(value, type); |
33 |
| - |
34 |
| - #endregion |
35 |
| - |
36 |
| - } |
37 |
| - |
38 |
| -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace GenHTTP.Modules.Conversion.Formatters |
| 5 | +{ |
| 6 | + |
| 7 | + public sealed class FormatterRegistry |
| 8 | + { |
| 9 | + |
| 10 | + #region Get-/Setters |
| 11 | + |
| 12 | + public IReadOnlyList<IFormatter> Formatters { get; private set; } |
| 13 | + |
| 14 | + #endregion |
| 15 | + |
| 16 | + #region Initialization |
| 17 | + |
| 18 | + public FormatterRegistry(List<IFormatter> formatters) |
| 19 | + { |
| 20 | + Formatters = formatters; |
| 21 | + } |
| 22 | + |
| 23 | + #endregion |
| 24 | + |
| 25 | + #region Functionality |
| 26 | + |
| 27 | + public bool CanHandle(Type type) |
| 28 | + { |
| 29 | + for (int i = 0; i < Formatters.Count; i++) |
| 30 | + { |
| 31 | + if (Formatters[i].CanHandle(type)) |
| 32 | + { |
| 33 | + return true; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + public object? Read(string value, Type type) |
| 41 | + { |
| 42 | + for (int i = 0; i < Formatters.Count; i++) |
| 43 | + { |
| 44 | + if (Formatters[i].CanHandle(type)) |
| 45 | + { |
| 46 | + return Formatters[i].Read(value, type); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public string? Write(object value, Type type) |
| 55 | + { |
| 56 | + for (int i = 0; i < Formatters.Count; i++) |
| 57 | + { |
| 58 | + if (Formatters[i].CanHandle(type)) |
| 59 | + { |
| 60 | + return Formatters[i].Write(value, type); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + #endregion |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments