Merge pull request #1225 from strawd/bug22307
[mono.git] / mcs / class / corlib / coreclr / FormattableStringFactory.cs
1 // Copyright (c) Microsoft. All rights reserved. 
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 
3
4 /*============================================================
5 **
6 ** Class:  FormattableStringFactory
7 **
8 **
9 ** Purpose: implementation of the FormattableStringFactory
10 ** class.
11 **
12 ===========================================================*/
13 namespace System.Runtime.CompilerServices
14 {
15     /// <summary>
16     /// A factory type used by compilers to create instances of the type <see cref="FormattableString"/>.
17     /// </summary>
18     public static class FormattableStringFactory
19     {
20         /// <summary>
21         /// Create a <see cref="FormattableString"/> from a composite format string and object
22         /// array containing zero or more objects to format.
23         /// </summary>
24         public static FormattableString Create(string format, params object[] arguments)
25         {
26             if (format == null)
27             {
28                 throw new ArgumentNullException("format");
29             }
30
31             if (arguments == null)
32             {
33                 throw new ArgumentNullException("arguments");
34             }
35
36             return new ConcreteFormattableString(format, arguments);
37         }
38
39         private sealed class ConcreteFormattableString : FormattableString
40         {
41             private readonly string _format;
42             private readonly object[] _arguments;
43
44             internal ConcreteFormattableString(string format, object[] arguments)
45             {
46                 _format = format;
47                 _arguments = arguments;
48             }
49
50             public override string Format { get { return _format; } }
51             public override object[] GetArguments() { return _arguments; }
52             public override int ArgumentCount { get { return _arguments.Length; } }
53             public override object GetArgument(int index) { return _arguments[index]; }
54             public override string ToString(IFormatProvider formatProvider) { return string.Format(formatProvider, _format, _arguments); }
55         }
56     }
57 }