让我们列出你发布优秀和最喜欢的扩展方法的答案.
要求是必须发布完整代码并提供示例和如何使用它的说明.
基于对该主题的高度关注,我在Codeplex上设置了一个名为extensionoverflow的开源项目.
请标记您的答案并接受将代码放入Codeplex项目中.
请发布完整的源代码而不是链接.
Codeplex新闻:
24.08.2010 Codeplex页面现在在这里:http://extensionoverflow.codeplex.com/
11.11.2008 XmlSerialize/XmlDeserialize现在已实现并且已经过单元测试.
11.11.2008仍有更多开发人员的空间.;-) 现在加入!
11.11.2008第三个贡献者加入了ExtensionOverflow,欢迎来到BKristensen
11.11.2008 FormatWith现在已实现且已经过单元测试.
09.11.2008第二个贡献者加入了ExtensionOverflow.欢迎来到chakrit.
09.11.2008我们需要更多开发人员.;-)
假设您要输出或连接字符串.您更喜欢以下哪种款式?
var p = new { FirstName = "Bill", LastName = "Gates" };
Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
Console.WriteLine(p.FirstName + " " + p.LastName);
你更喜欢使用格式还是简单地连接字符串?什么是你最喜欢的?其中一个伤害了你的眼睛吗?
你有任何理性的论据来使用一个而不是另一个吗?
我会去第二个.
在.NET中,我可以提供两者\r或\n字符串文字,但有一种方法可以插入像"new line"这样的特殊字符,比如Environment.NewLine静态属性?
这就是我所知道的
str = String.Format("Her name is {0} and she's {1} years old", "Lisa", "10");
Run Code Online (Sandbox Code Playgroud)
但我想要类似的东西
str = String("Her name is @name and she's @age years old");
str.addParameter(@name, "Lisa");
str.addParameter(@age, 10);
Run Code Online (Sandbox Code Playgroud) 是否可以在字符串格式说明符中添加一些描述性文本?
例:
string.Format ("{0:ForeName} is not at home", person.ForeName);
Run Code Online (Sandbox Code Playgroud)
在示例ForeName中添加了描述.
上面的语法显然是不正确的,但只是为了表明这个想法.
我问的原因是因为在我的情况下字符串是在资源文件中,所以在你目前只看到的资源文件中
{0} is not at home
Run Code Online (Sandbox Code Playgroud)
在某些情况下,很难掌握其背景{0}.
编辑:
在c#6中$引入了与运算符的字符串插值,因此string.Format不再需要:
$"{person.ForeName} is not at home";
Run Code Online (Sandbox Code Playgroud) 我正在使用FluentValidation,我想格式化一个对象的属性值的消息.问题是我对C#中的表达式和委托的经验很少.
FluentValidation已经提供了一种使用格式参数执行此操作的方法.
RuleFor(x => x.Name).NotEmpty()
.WithMessage("The name {1} is not valid for Id {0}", x => x.Id, x => x.Name);
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情,以避免在我更改参数的顺序时更改消息字符串.
RuleFor(x => x.Name).NotEmpty()
.WithMessage("The name {Name} is not valid for Id {Id}",
x => new
{
Id = x.Id,
Name = x.Name
});
Run Code Online (Sandbox Code Playgroud)
原始方法签名如下所示:
public static IRuleBuilderOptions<T, TProperty> WithMessage<T, TProperty>(
this IRuleBuilderOptions<T, TProperty> rule, string errorMessage,
params Func<T, object>[] funcs)
Run Code Online (Sandbox Code Playgroud)
我在考虑为这个方法提供一个Func列表.
有人可以帮我这个吗?
我有一个模板字符串和一个参数数组来自不同的来源,但需要匹配以创建一个新的"填充"字符串:
string templateString = GetTemplate(); // e.g. "Mr {0} has a {1}"
string[] dataItems = GetDataItems(); // e.g. ["Jones", "ceiling cat"}
string resultingString = String.Format(templateString, dataItems);
// e.g. "Mr Jones has a ceiling cat"
Run Code Online (Sandbox Code Playgroud)
使用此代码,我假设模板中字符串格式占位符的数量将等于数据项的数量.在我的情况下,这通常是一个公平的假设,但resultingString即使假设是错误的,我也希望能够产生一个不失败的假设.我不介意是否有空格来丢失数据.
如果项目太多dataItems,该String.Format方法处理得很好.如果还不够,我会得到一个例外.
为了解决这个问题,我计算占位符的数量,并在dataItems数组中添加新项目(如果没有足够的话).
为了计算占位符,我目前正在处理的代码是:
private static int CountOccurrences(string haystack)
{
// Loop through all instances of the string "}".
int count = 0;
int i = 0;
while ((i = text.IndexOf("}", i)) != -1)
{
i++;
count++;
}
return count; …Run Code Online (Sandbox Code Playgroud) 我们有一个模板网址,如:
http://api.example.com/sale?auth_user=xxxxx&auth_pass=xxxxx&networkid={networkid}&category=b2c&country=IT&pageid={pageid}&programid=133&saleid=1&m={master}&optinfo={optinfo}&publisher={publisher}&msisdn={userId}
Run Code Online (Sandbox Code Playgroud)
我有这些常数令牌的价值.如何在C#中替换所有这些令牌?
参考此链接
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx
看起来String.Format方法不可能在格式字符串参数中使用标签而不是索引.
.Net Framework是否有一些原生的语法或功能允许使用标签而不是0,1,...,N?
作为一个例子,这怎么可能:
Console.WriteLine(String.Format("{0}{1}{0}{1}", "foo", "bar"));
Run Code Online (Sandbox Code Playgroud)
成为这样的事情:
Console.WriteLine(String.Format("{foo}{bar}{foo}{bar}",
new { foo = "foo", bar = "bar" }));
Run Code Online (Sandbox Code Playgroud) 我已经开始使用Interpolated Strings(C#6的新功能),它非常有用且优雅.但根据我的需要,我必须将字符串格式作为参数传递给方法.像下一个:
MyMethod(string format)
Run Code Online (Sandbox Code Playgroud)
在过去,我在下一个方面使用它:
MyMethod("AAA{0:00}")
Run Code Online (Sandbox Code Playgroud)
现在我尝试了这段代码:
MyMethod($"AAA{i:00}")
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它i是在方法内部创建的,并且在此上下文中超出了范围.
是否可以使用任何技巧将插值字符串作为参数传递给方法?
有没有办法在String.Format没有指定{1}, {2},等的情况下发送消息?是否可以采用某种形式的自动增量?(与普通老年人相似printf)
c# ×11
.net ×7
string ×4
asp.net ×1
c#-6.0 ×1
coding-style ×1
formatting ×1
func ×1
line-breaks ×1
linq ×1
open-source ×1
token ×1