|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel.DataAnnotations; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace IGLib.Alt; |
| 8 | + |
| 9 | + |
| 10 | +public class DefaultFormatProviderSelector |
| 11 | +{ |
| 12 | + |
| 13 | + /// <summary>Returns the global <see cref="IDefaultFormatProviderSelector"/> that is used to provide |
| 14 | + /// the <see cref="IFormatProvider"/> when one is not specified, and also the the |
| 15 | + /// <see cref="IDefaultFormatProviderSelector"/> is not specified.</summary> |
| 16 | + public static IDefaultFormatProviderSelector Global { get; private set; } |
| 17 | + = new DefaultFormatProviderSelectorInvariantCulture(); |
| 18 | + |
| 19 | + /// <summary>Sets (changes) the global <see cref="IDefaultFormatProviderSelector"/>, which will be |
| 20 | + /// used by default to select the default <see cref="IFormatProvider"/> object when one is not |
| 21 | + /// specified, and the <see cref="IDefaultFormatProviderSelector"/> to obtain the default value |
| 22 | + /// is also not specified.</summary> |
| 23 | + /// <param name="selector">The <see cref="IDefaultFormatProviderSelector"/> that will be returned |
| 24 | + /// by the <see cref="DefaultFormatProviderSelector.Global"/> property after this call. |
| 25 | + /// The marameter may not be null (otherwise, this method throws an exception).</param> |
| 26 | + /// <exception cref="ArgumentNullException">When the <paramref name="selector"/> parameter is null.</exception> |
| 27 | + public static void SetGlobal(IDefaultFormatProviderSelector selector) |
| 28 | + { |
| 29 | + if (selector == null) |
| 30 | + { |
| 31 | + throw new ArgumentNullException(nameof(selector), "The global selector of default format provider cannot be set to null."); |
| 32 | + } |
| 33 | + Global = selector; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary>Returns the appropriate <see cref="IFormatProvider"/> according to the specified (suggested) |
| 37 | + /// <paramref name="specifiedFormatProvider"/> (that can be null) and the specified <paramref name="defaultSelector"/>, |
| 38 | + /// which provides the default <see cref="IFormatProvider"/> when the <paramref name="specifiedFormatProvider"/> |
| 39 | + /// is null If both parameters are null then the default <see cref="IFormatProvider"/> is provided by the |
| 40 | + /// <see cref="DefaultFormatProviderSelector.Global"/> property.</summary> |
| 41 | + /// <param name="specifiedFormatProvider">The specified <see cref="IFormatProvider"/>. If not null then this |
| 42 | + /// object is returned immediately, otherwise the <paramref name="defaultSelector"/> parameter is used to |
| 43 | + /// provide the object that is returned.</param> |
| 44 | + /// <param name="defaultSelector">Provides the default <see cref="IFormatProvider"/> (via the |
| 45 | + /// <see cref="IDefaultFormatProviderSelector.DefaultFormatProvider"/> property) in case that the <paramref name="specifiedFormatProvider"/> |
| 46 | + /// is null. In that case, if this parameter is also null, the <see cref="DefaultFormatProviderSelector.Global"/> provides the |
| 47 | + /// default object to be returned.</param> |
| 48 | + /// <returns>The <see cref="IFormatProvider"/> returned according to method parameters: |
| 49 | + /// <para>* If <paramref name="specifiedFormatProvider"/> is not null, this object is returned.</para> |
| 50 | + /// <para>* Otherwise, if <paramref name="defaultSelector"/> is not null then the object obtained by its |
| 51 | + /// <see cref="IDefaultFormatProviderSelector.DefaultFormatProvider"/> property is returned.</para> |
| 52 | + /// <para>Otherwise, the format provider obtained by the <see cref="DefaultFormatProviderSelector.Global"/>'s |
| 53 | + /// <see cref="IDefaultFormatProviderSelector.DefaultFormatProvider"/> property is returned.</para></returns> |
| 54 | + /// <remarks>Default parameters are intentionally not provided, such that the caller needs to consciously |
| 55 | + /// provide null values if this is itended.</remarks> |
| 56 | + [Obsolete("Replaced with ResolveFormatProvider(IFormatProvider?, IDefaultFormatProviderSelector?)")] |
| 57 | + public static IFormatProvider GetFormatProvider(IFormatProvider? specifiedFormatProvider, |
| 58 | + IDefaultFormatProviderSelector? defaultSelector) |
| 59 | + { |
| 60 | + return (specifiedFormatProvider ?? (defaultSelector ?? Global).DefaultFormatProvider); |
| 61 | + } |
| 62 | + |
| 63 | + public static IFormatProvider ResolveFormatProvider( |
| 64 | + IFormatProvider? specifiedFormatProvider, |
| 65 | + IDefaultFormatProviderSelector? defaultSelector) |
| 66 | + { |
| 67 | + return specifiedFormatProvider ?? (defaultSelector ?? Global).DefaultFormatProvider; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +} |
0 commit comments