ProjectArcade
77 строк · 3.0 Кб
1using System;
2
3namespace DokanNet
4{
5/// <summary>
6/// Provide support to format object with <c>null</c>.
7/// </summary>
8public class FormatProviders : IFormatProvider, ICustomFormatter
9{
10/// <summary>
11/// A Singleton instance of this class.
12/// </summary>
13public static readonly FormatProviders DefaultFormatProvider = new FormatProviders();
14
15/// <summary>
16/// A constant string that represents what to use if the formated object is <c>null</c>.
17/// </summary>
18public const string NullStringRepresentation = "<null>";
19
20/// <summary>
21/// Prevents a default instance of the <see cref="FormatProviders"/> class from being created.
22/// </summary>
23private FormatProviders()
24{
25}
26
27/// <summary>
28/// Format a <see cref="FormattableString"/> using <see cref="DefaultFormatProvider"/>.
29/// </summary>
30/// <param name="formattable">The <see cref="FormattableString"/> to format.</param>
31/// <returns>The formated string.</returns>
32#pragma warning disable 3001
33public static string DokanFormat(string formattable)
34{
35return formattable; //.ToString(DefaultFormatProvider);
36}
37#pragma warning restore 3001
38
39/// <summary>
40/// Returns an object that provides formatting services for the
41/// specified type.
42/// </summary>
43/// <returns>An instance of the object specified by
44/// <paramref name="formatType" />, if the
45/// <see cref="T:System.IFormatProvider" /> implementation can supply
46/// that type of object; otherwise, <c>null</c>.</returns>
47/// <param name="formatType">An object that specifies the type of format
48/// object to return. </param>
49public object GetFormat(Type formatType)
50{
51return formatType == typeof(ICustomFormatter) ? this : null;
52}
53
54/// <summary>
55/// Converts the value of a specified object to an equivalent string
56/// representation using specified format and culture-specific
57/// formatting information.
58/// </summary>
59/// <returns>The string representation of the value of
60/// <paramref name="arg" />, formatted as specified by
61/// <paramref name="format" /> and <paramref name="formatProvider" />.
62/// </returns>
63/// <param name="format">A format string containing formatting
64/// specifications. </param>
65/// <param name="arg">An object to format. </param>
66/// <param name="formatProvider">An object that supplies format
67/// information about the current instance. </param>
68public string Format(string format, object arg, IFormatProvider formatProvider)
69{
70if (arg == null)
71return NullStringRepresentation;
72
73var formattable = arg as IFormattable;
74return formattable != null ? formattable.ToString(format, formatProvider) : arg.ToString();
75}
76}
77}