如何阻止ReSharper在指定的变量和链式方法调用之间插入不必要的换行符
我有一个类有一些返回this
方法,这样我可以将调用链接在一起:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Person WithFirstName(string firstName) { FirstName = firstName; return this; } public Person WithLastName(string lastName) { LastName = lastName; return this; } }
但是,当我将足够的方法链接在一起以便在ReSharper中设置Right margin (columns)
,表达式的格式有点时髦。
var john = new Person().WithFirstName("John") .WithLastName("Smith") .WithLastName("Smith") .WithLastName("Smith") .WithLastName("Smith");
显然,这个例子有点人为,因为我永远不会一遍又一遍地调用相同的方法,但在我的生产代码中,我正在与一个Fluent API连接,该API通过将大约10个方法调用链接在一起来配置。
理想情况下,代码的格式如下:
// on a single line when less characters than `Right margin (columns)` var john = new Person().WithFirstName("John").WithLastName("Smith"); // aligned at the "." when longer than `Right margin (columns)` var john = new Person().WithFirstName("John") .WithLastName("Smith") .WithLastName("Smith") .WithLastName("Smith") .WithLastName("Smith");
真正的问题是为什么ReSharper将整个表达放在一个新的界限上。 有什么建议?
您需要将“Wrap chained method calls”设置为“Chop always”,以便在同一行上获得var john = new Person().WithFirstName("John")
。
我不确定哪一个负责,你需要调整一下,但格式化设置放在Reshaper-> Options-> Code Editing-> C# – > Formatting Style->换行和换行中。 这对你来说至少是一个很好的起点!
- 自定义Visual Studio
- 虚拟和物理内存/ OutOfMemoryException
- 使用GetResponseStream readToEnd读取HTTPWebReponse返回奇怪的字符
- entity framework在where子句中添加了额外的条件
- 方法“X.set_DbConnection(System.Data.Common.DbConnection)”错误尝试访问方法“Y.get_Settings()”失败
- 如何将方法名称传递给另一个方法并通过委托变量调用它?
- c#LINQ:如何检索单个结果
- 如何使Newtonsoft.Json.Linq.JObject不可变?
- 剃刀 – 绑定问题与单选按钮