Skip to content

Instantly share code, notes, and snippets.

@skyline75489
Created May 8, 2017 06:49
Show Gist options
  • Save skyline75489/27d5c28cd8c075f805d2de187efef3d2 to your computer and use it in GitHub Desktop.
Save skyline75489/27d5c28cd8c075f805d2de187efef3d2 to your computer and use it in GitHub Desktop.
FormattableString on .NET 4.0
namespace System
{
using System.Globalization;
public class FormattableString : IFormattable
{
private readonly string s;
private readonly object[] args;
public FormattableString(string s, object[] args)
{
this.s = s;
this.args = args;
}
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, s, args);
}
string IFormattable.ToString(string ignored, IFormatProvider formatProvider) => this.ToString(formatProvider);
public override string ToString() => this.ToString(CultureInfo.CurrentCulture);
public static string Invariant(FormattableString formattable)
{
if (formattable == null)
{
throw new ArgumentNullException(nameof(formattable));
}
return formattable.ToString(CultureInfo.InvariantCulture);
}
}
}
namespace System.Runtime.CompilerServices
{
internal static class FormattableStringFactory
{
public static FormattableString Create(string s, params object[] args)
{
return new FormattableString(s, args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment