C# 字符串格式的描述性文本

C# 字符串格式的描述性文本,c#,C#,是否可以向字符串格式说明符添加一些描述性文本 例如: string.Format ("{0:ForeName} is not at home", person.ForeName); 在示例中,ForeName被添加为描述 上面的语法显然是不正确的,但只是为了说明这一点 我之所以问这个问题,是因为在我的例子中,字符串在资源文件中,所以在资源文件中,您当前只看到字符串 {0} is not at home 在某些情况下,很难理解{0}的上下文是什么 编辑: 在c#6中,引入了带有$运

是否可以向字符串格式说明符添加一些描述性文本

例如:

  string.Format ("{0:ForeName} is not at home", person.ForeName); 
在示例中,
ForeName
被添加为描述

上面的语法显然是不正确的,但只是为了说明这一点

我之所以问这个问题,是因为在我的例子中,字符串在资源文件中,所以在资源文件中,您当前只看到字符串

   {0} is not at home
在某些情况下,很难理解
{0}
的上下文是什么

编辑:

在c#6中,引入了带有
$
运算符的字符串插值,因此不再需要
string.Format

$"{person.ForeName} is not at home";
这将打印名字而不是{0}和{1}中的某物。没有办法像你说的那样。没有内置的C#函数。我能提出的最好建议是插入一条评论(这不会对性能产生影响):


就个人而言,我觉得它不可读,最好的方法是使用David Khaykin在评论中建议的第三方工具(请参见)

对于字符串,您的方法应该可以工作,因为字符串将忽略任何格式说明符。但是,您可能会意外地将其用于非字符串类型,在这种情况下,字符串将被转换为格式代码或按字面显示:

string.Format ("{0:ForeName} is not at home", "Johnny");  
//"Johnny is not at home"

string.Format ("{0:ForeName} will be home at {1:HomeTime}", "Johnny", DateTime.Today)
//Johnny will be home at 0o0eTi0e  -- H, h, and m are DateTime format codes.

但是,由于您将这些内容存储在资源文件中,因此我将使用资源文件中的“注释”字段-您可以存储格式字符串的副本并在其中添加说明。

我们通常将注释放入资源文件中,例如
{0}=Forename

然后,可能正在翻译字符串的任何人都知道
{0}
代表什么,并且可以相应地进行翻译


另外,如果使用ReSharper,可以在将字符串添加到资源的同时输入注释。

Phil Haack和Peli写了几篇有趣的博客文章,介绍了默认string.format函数的替代方法。你可能会感兴趣

基本上,它们允许您在格式字符串中使用对象属性,如下所示:

string s = NamedFormat("Hello {FullName} ({EmailAdrress})!", person);
您可以在此处查看相关的博客帖子:

也许这些博客文章中介绍的解决方案之一适合您的需要。

下面是StackOverflow的
formatUnicorn
方法的一个有点幼稚的实现:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;

public class Test
{
    public static void Main()
    {
        string formatString = "{firstName} {lastName} is awesome.";

        Console.WriteLine(formatString.FormatUnicorn(new {
            firstName = "joe",
            lastName = "blow"
        }));
    }
}

public static class StringExtensions {
    public static string FormatUnicorn(this string str, object arguments) {
        string output = str;

        Type type = arguments.GetType();

        foreach (PropertyInfo property in type.GetProperties())
        {
            Regex regex = new Regex(@"\{" + property.Name + @"\}");
            output = regex.Replace(output, property.GetValue(arguments, null).ToString());
        }

        return output;
    }
}
这里最大的缺点是使用反射,这可能很慢。另一个是它不允许使用格式说明符


更好的方法可能是创建一个更复杂的正则表达式,只删除注释。

从Visual Studio 2015开始,您可以使用它(这是一个编译器技巧,所以您针对的是.net framework的哪个版本并不重要)

代码如下所示

string txt = $"{person.ForeName} is not at home {person.Something}"; 

如果您想将字符串放入资源文件中进行翻译,这并不理想,但它通常会使代码更具可读性,更不容易出错。

您想让正在阅读代码的开发人员知道
{0}
中发生了什么,就是这样吗?一种注释?这里的意图是什么?只需谷歌“字符串格式命名参数”。没有本地的方法,只有变通办法@crush:听起来它的目的是在更大的格式字符串上使用直观的命名,使代码更具可读性。本质上,将格式字符串占位符视为变量(类似于SQL参数占位符)。它在NuGet上提供。不过,我从未在.NET中使用过它。这可能有点过头了。谢谢,但重点是描述应该在字符串中,所以它会进入资源文件。我喜欢这部分的“无性能影响”部分。但是作者说的是如果资源文件。我们可以在资源文件中添加注释吗?@Johnny5可以在resx文件条目中添加注释,因此在其中添加注释是一个可行的选择。当我回答时,没有提到resx合同。我认为Jandic给出了最好的答案有时候最好的解决方案更简单
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;

public class Test
{
    public static void Main()
    {
        string formatString = "{firstName} {lastName} is awesome.";

        Console.WriteLine(formatString.FormatUnicorn(new {
            firstName = "joe",
            lastName = "blow"
        }));
    }
}

public static class StringExtensions {
    public static string FormatUnicorn(this string str, object arguments) {
        string output = str;

        Type type = arguments.GetType();

        foreach (PropertyInfo property in type.GetProperties())
        {
            Regex regex = new Regex(@"\{" + property.Name + @"\}");
            output = regex.Replace(output, property.GetValue(arguments, null).ToString());
        }

        return output;
    }
}
string txt = $"{person.ForeName} is not at home {person.Something}";