在Visual Studio 2015年的IFormatProvider(CA1305)字符串插值 [英] String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

查看:326
本文介绍了在Visual Studio 2015年的IFormatProvider(CA1305)字符串插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2015年新的字符串插值的风格是这样的:

 暗淡S = $您好(名称)
 

但是,如果我用这个code分析告诉我,我打破 CA1305 :指定的IFormatProvider

在老的时候,我做了这样的:

 暗淡S =的String.Format(Globalization.CultureInfo.InvariantCulture,你好{0},名称)
 

但怎么能与新的风格呢?

我要指出,我正在寻找一个解决方案,对于.NET 4.5.2(用于.NET 4.6 dcastro有答案

解决方案

您会使用 System.FormattableString System.IFormattable 类:

  IFormattable IFS =(IFormattable)$你好,(名称);
System.FormattableString FSS = $您好,(名称);

//传递null要使用的格式,因为它是在初始化时使用以上。
字符串ifresult = ifs.ToString(空,CultureInfo.InvariantCulture);
字符串fsresult = fss.ToString(CultureInfo.InvariantCulture);
 

您必须编写针对框架4.6,为 IFromattable FormattableString 是不中不存在的类旧的版本。因此,如果您的目标较旧版本的.NET框架您不触发错误不能使用插语法。

除非你申请一个小黑客(适应编译免受乔恩斯基特的要点 4.6 RTM和的forked我自己的帐户)。只是一个类文件添加到包含项目:

  

更新

     

现在还有一个的NuGet封装,将提供相同的功能,以您的项目(谢谢你把这个我注意到@habakuk)。

 安装包StringInterpolationBridge
 

或者,如果你想达到同样的事情,而不增加额外的组件,您的产品添加以下code到项目:

 命名空间System.Runtime.CompilerServices
{
    内部类FormattableStringFactory
    {
        公共静态FormattableString创建(字符串的MessageFormat,params对象[]参数)
        {
            返回新FormattableString(的MessageFormat,参数);
        }
    }
}

命名空间系统
{
    内部类FormattableString:IFormattable
    {
        私人只读字符串的MessageFormat;
        私人只读对象[] ARGS;

        公共FormattableString(字符串的MessageFormat,对象[]参数)
        {
            this.messageFormat =的MessageFormat;
            this.args = ARGS;
        }

        公共重写字符串的ToString()
        {
            返回的String.Format(的MessageFormat,参数);
        }

        公共字符串的ToString(字符串格式,的IFormatProvider formatProvider)
        {
            返回的String.Format(formatProvider,格式??的MessageFormat,参数);
        }

        公共字符串的ToString(IFormatProvider的formatProvider)
        {
            返回的String.Format(formatProvider,的MessageFormat,参数);
        }
    }
}
 

请参阅:

  • <一个href="https://msdn.microsoft.com/en-us/library/dn961160.aspx">https://msdn.microsoft.com/en-us/library/dn961160.aspx

The new string interpolation style in Visual Studio 2015 is this:

Dim s = $"Hello {name}"

But if I use this the code analysis tells me I break CA1305: Specify IFormatProvider

In the old times I did it like this:

Dim s = String.Format(Globalization.CultureInfo.InvariantCulture, "Hello {0}", name)

But how can it be done with the new style?

I have to mention that I'm looking for a solution for .Net 4.5.2 (for .Net 4.6 dcastro has the answer)

解决方案

You'd use the System.FormattableString or System.IFormattable class:

IFormattable ifs = (IFormattable)$"Hello, {name}";
System.FormattableString fss = $"Hello, {name}";

// pass null to use the format as it was used upon initialization above.
string ifresult = ifs.ToString(null, CultureInfo.InvariantCulture);
string fsresult = fss.ToString(CultureInfo.InvariantCulture);

You need to be compiling against Framework 4.6, as the IFromattable and FormattableString are classes which do not exist in older versions. So if you're targeting older versions of the .NET framework you can't use the interpolation syntax without triggering the error.

Unless you apply a little hack (adapted to compile against 4.6 RTM from Jon Skeet's gist and forked to my own account.). Just add a class file to your project containing:

Update

There is now also a Nuget package available that will provide the same functionality to your project (thanks for bringing this to my attention @habakuk).

install-package StringInterpolationBridge

Or if you want to achieve the same thing without adding an additional assembly to your product add the following code to your project:

namespace System.Runtime.CompilerServices
{
    internal class FormattableStringFactory
    {
        public static FormattableString Create(string messageFormat, params object[] args)
        {
            return new FormattableString(messageFormat, args);
        }
    }
}

namespace System
{
    internal class FormattableString : IFormattable
    {
        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);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            return string.Format(formatProvider, format ?? messageFormat, args);
        }

        public string ToString(IFormatProvider formatProvider)
        {
            return string.Format(formatProvider, messageFormat, args);
        }
    }
}

See:

这篇关于在Visual Studio 2015年的IFormatProvider(CA1305)字符串插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆