TazmanianTad
❔ Allowing a user to use a custom format in string.format
Currently trying this, it's a little closer but I can't seem to get the "embedded" format to work:
public class MyFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string fmt, object arg, IFormatProvider formatProvider)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
if(fmt != null)
{
Console.WriteLine($"FORMAT: {fmt}");
}
if (arg != null)
{
Console.WriteLine($"ARG: {arg}");
}
if (arg == null) return string.Empty;
if (fmt == null) return arg.ToString();
if (fmt.StartsWith("x"))
fmt = fmt.Substring(1);
return string.Format(fmt, arg);
}
}
public class MyFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string fmt, object arg, IFormatProvider formatProvider)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
if(fmt != null)
{
Console.WriteLine($"FORMAT: {fmt}");
}
if (arg != null)
{
Console.WriteLine($"ARG: {arg}");
}
if (arg == null) return string.Empty;
if (fmt == null) return arg.ToString();
if (fmt.StartsWith("x"))
fmt = fmt.Substring(1);
return string.Format(fmt, arg);
}
}
3 replies