Skip to content

Instantly share code, notes, and snippets.

@jskeet
Created February 23, 2015 21:31
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jskeet/9d297d0dc013d7a557ee to your computer and use it in GitHub Desktop.
// This is a .NET 3.5 console app, compiled with the C# 6 compiler from CTP6
using System;
namespace System.Runtime.CompilerServices
{
public class FormattableStringFactory
{
public static FormattableString Create(string messageFormat, params object[] args)
{
return new FormattableString(messageFormat, args);
}
public static FormattableString Create(string messageFormat, DateTime bad, params object[] args)
{
var realArgs = new object[args.Length + 1];
realArgs[0] = "Please don't use DateTime";
Array.Copy(args, 0, realArgs, 1, args.Length);
return new FormattableString(messageFormat, realArgs);
}
}
}
namespace System
{
public class FormattableString
{
private readonly string messageFormat;
private readonly object[] args;
public FormattableString(string messageFormat, object[] args)
{
this.messageFormat = messageFormat;
this.args = args;
}
public override string ToString()
{
return string.Format(messageFormat, args);
}
}
}
namespace ConsoleApp35
{
class Program
{
static void Main(string[] args)
{
int x = 10;
FormattableString fs = $"{nameof(x)}={x}";
Console.WriteLine(fs);
fs = $"{DateTime.Now}: {nameof(x)}={x}";
Console.WriteLine(fs);
}
}
}
@paulomorgado
Copy link

@jskeet, FormattableString nneds to implement IFormattable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment