带字符串插值的重载字符串方法

Overloaded string methods with string interpolation

为什么字符串插值更喜欢使用 string 而不是 IFormattable 的方法重载?

假设如下:

static class Log {
    static void Debug(string message);
    static void Debug(IFormattable message);
    static bool IsDebugEnabled { get; }
}

我有非常昂贵的物品 ToString()。之前,我做了以下操作:

if (Log.IsDebugEnabled) Log.Debug(string.Format("Message {0}", expensiveObject));

现在,我想在 Debug(IFormattable) 中包含 IsDebugEnabled 逻辑,并仅在必要时对消息中的对象调用 ToString()。

Log.Debug($"Message {expensiveObject}");

但是,这会调用 Debug(string) 重载。

这是一个deliberate decision by the Roslyn team:

We generally believe that libraries will mostly be written with different API names for methods which do different things. Therefore overload resolution differences between FormattableString and String don't matter, so string might as well win. Therefore we should stick with the simple principle that an interpolated string is a string. End of story.

link 中对此有更多讨论,但结果是他们希望您使用不同的方法名称。

Some library APIs really want consumers to use FormattableString because it is safer or faster. The API that takes string and the API that takes FormattableString actually do different things and hence shouldn't be overloaded on the same name.

您需要将其转换为 IFormattableFormattableString:

Log.Debug((IFormattable)$"Message {expensiveObject}");

您可以使用 neet 技巧作为 shorthand 转换为 IFormattable:

public static class FormattableExtensions
{
    public static FormattableString FS(FormattableString formattableString)
    {
        return formattableString;
    }
}

并这样使用:

Log.Debug(FS($"Message {expensiveObject}"));

我希望 JIT 编译器在生产中内联 FS

意识到你问为什么你不能这样做,我只想指出你实际上可以这样做。

您只需要诱使编译器优先使用 FormattableString 重载。我在这里详细解释了它:https://robertengdahl.blogspot.com/2016/08/how-to-overload-string-and.html

这里是测试代码:

public class StringIfNotFormattableStringAdapterTest
{
    public interface IStringOrFormattableStringOverload
    {
        void Overload(StringIfNotFormattableStringAdapter s);
        void Overload(FormattableString s);
    }

    private readonly IStringOrFormattableStringOverload _stringOrFormattableStringOverload =
        Substitute.For<IStringOrFormattableStringOverload>();

    public interface IStringOrFormattableStringNoOverload
    {
        void NoOverload(StringIfNotFormattableStringAdapter s);
    }

    private readonly IStringOrFormattableStringNoOverload _noOverload =
        Substitute.For<IStringOrFormattableStringNoOverload>();

    [Fact]
    public void A_Literal_String_Interpolation_Hits_FormattableString_Overload()
    {
        _stringOrFormattableStringOverload.Overload($"formattable string");
        _stringOrFormattableStringOverload.Received().Overload(Arg.Any<FormattableString>());
    }

    [Fact]
    public void A_String_Hits_StringIfNotFormattableStringAdapter_Overload()
    {
        _stringOrFormattableStringOverload.Overload("plain string");
        _stringOrFormattableStringOverload.Received().Overload(Arg.Any<StringIfNotFormattableStringAdapter>());
    }

    [Fact]
    public void An_Explicit_FormattableString_Detects_Missing_FormattableString_Overload()
    {
        Assert.Throws<InvalidOperationException>(
            () => _noOverload.NoOverload((FormattableString) $"this is not allowed"));
    }
}

下面是实现此功能的代码:

public class StringIfNotFormattableStringAdapter
{
    public string String { get; }

    public StringIfNotFormattableStringAdapter(string s)
    {
        String = s;
    }

    public static implicit operator StringIfNotFormattableStringAdapter(string s)
    {
        return new StringIfNotFormattableStringAdapter(s);
    }

    public static implicit operator StringIfNotFormattableStringAdapter(FormattableString fs)
    {
        throw new InvalidOperationException(
            "Missing FormattableString overload of method taking this type as argument");
    }
}

您不能强制编译器选择 IFormattable/FormattableString 而不是 String,但您可以让它选择 IFormattable/FormattableString 而不是 Object:

static class Log
{
    static void Debug(object message);
    static void Debug(IFormattable message);
    static void Debug(FormattableString message);
    static bool IsDebugEnabled { get; }
}

此解决方案的成本是在采用 Object 的方法中进行额外的 ToString() 调用。 (FormattableString 的额外重载并不是真正必要的,但会简化查找使用内插字符串的位置)