我从ReSharper收到一条关于从我的对象构造函数调用虚拟成员的警告.
为什么不做这件事?
Resharper喜欢指出每个asp.net页面可以静态化的多个函数.如果我让它们静止,对我有帮助吗?我应该将它们设置为静态并将它们移动到实用程序类吗?
在我的代码中需要使用IEnumerable<>
几次因此得到Resharper错误"可能的多个枚举IEnumerable
".
示例代码:
public List<object> Foo(IEnumerable<object> objects)
{
if (objects == null || !objects.Any())
throw new ArgumentException();
var firstObject = objects.First();
var list = DoSomeThing(firstObject);
var secondList = DoSomeThingElse(objects);
list.AddRange(secondList);
return list;
}
Run Code Online (Sandbox Code Playgroud)
objects
参数List
,然后避免可能的多次枚举,但后来我没有得到我能处理的最高对象. IEnumerable
到List
在方法的开头: public List<object> Foo(IEnumerable<object> objects)
{
var objectList = objects.ToList();
// ...
}
Run Code Online (Sandbox Code Playgroud)
但这只是尴尬.
在这种情况下你会做什么?
我安装了ReSharper,它可以在Visual Studio中运行,但如何禁用它?
每当我在ReSharper菜单中搜索时,我都找不到禁用选项.
string [] files = new string[2];
files[0] = "ThinkFarAhead.Example.Settings.Configuration_Local.xml";
files[1] = "ThinkFarAhead.Example.Settings.Configuration_Global.xml";
//Resharper complains this is an "access to modified closure"
for (int i = 0; i < files.Length; i++ )
{
// Resharper disable AccessToModifiedClosure
if(Array.Exists(Assembly.GetExecutingAssembly().GetManifestResourceNames(),
delegate(string name) { return name.Equals(files[i]); }))
return Assembly.GetExecutingAssembly().GetManifestResourceStream(files[i]);
// ReSharper restore AccessToModifiedClosure
}
Run Code Online (Sandbox Code Playgroud)
虽然ReSharper抱怨这是"访问修改后的闭包",但上述情况似乎工作正常.任何人都可以阐明这一点吗?
(这个主题在这里继续)
我有以下代码:
public double CalculateDailyProjectPullForceMax(DateTime date, string start = null, string end = null)
{
Log("Calculating Daily Pull Force Max...");
var pullForceList = start == null
? _pullForce.Where((t, i) => _date[i] == date).ToList() // implicitly captured closure: end, start
: _pullForce.Where(
(t, i) => _date[i] == date && DateTime.Compare(_time[i], DateTime.Parse(start)) > 0 &&
DateTime.Compare(_time[i], DateTime.Parse(end)) < 0).ToList();
_pullForceDailyMax = Math.Round(pullForceList.Max(), 2, MidpointRounding.AwayFromZero);
return _pullForceDailyMax;
}
Run Code Online (Sandbox Code Playgroud)
现在,我在ReSharper建议改变的行上添加了评论.这是什么意思,或者为什么需要改变?implicitly captured closure: end, start
通常我想检查提供的值是否与列表中的值匹配(例如,在验证时):
if (!acceptedValues.Any(v => v == someValue))
{
// exception logic
}
Run Code Online (Sandbox Code Playgroud)
最近,我注意到ReSharper要求我将这些查询简化为:
if (acceptedValues.All(v => v != someValue))
{
// exception logic
}
Run Code Online (Sandbox Code Playgroud)
显然,这在逻辑上是相同的,可能稍微更具可读性(如果你已经做了很多数学),我的问题是:这是否会导致性能下降?
它感觉应该是这样(即.Any()
听起来像是短路,而.All()
听起来不是这样),但我没有任何证据证明这一点.有没有人更深入地了解查询是否会解决相同的问题,或者ReSharper是否让我误入歧途?
当我在我的代码上运行ReSharper时,例如:
if (some condition)
{
Some code...
}
Run Code Online (Sandbox Code Playgroud)
ReSharper给了我上面的警告(反转"if"声明以减少嵌套),并提出了以下更正:
if (!some condition) return;
Some code...
Run Code Online (Sandbox Code Playgroud)
我想明白为什么那样更好.我一直认为在方法中间使用"返回"有问题,有点像"goto".
public class EnumRouteConstraint<T> : IRouteConstraint
where T : struct
{
private static readonly Lazy<HashSet<string>> _enumNames; // <--
static EnumRouteConstraint()
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException(
Resources.Error.EnumRouteConstraint.FormatWith(typeof(T).FullName));
}
string[] names = Enum.GetNames(typeof(T));
_enumNames = new Lazy<HashSet<string>>(() => new HashSet<string>
(
names.Select(name => name), StringComparer.InvariantCultureIgnoreCase
));
}
public bool Match(HttpContextBase httpContext, Route route,
string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
bool match = _enumNames.Value.Contains(values[parameterName].ToString());
return match;
}
}
Run Code Online (Sandbox Code Playgroud)
这是错的吗?我会假设这实际上有一个static readonly
字段用于EnumRouteConstraint<T>
我碰巧实例化的每一个.
我在C#解决方案上遇到Visual Studio问题.它显示完全随机的错误,但项目构建.现在,我有33个文件有错误,我可以在所有文件中看到红色的波浪线.
我尝试清理/重建解决方案,关闭Visual Studio甚至重新启动计算机.我还确保执行调试运行中描述的步骤,即使编译器在Visual Studio中有错误也是如此.我可以修改.cs文件,我看到解决方案中的变化.
有没有人知道它为什么这样做?
resharper ×10
c# ×9
.net ×3
linq ×2
performance ×2
closures ×1
constructor ×1
generics ×1
intellisense ×1
refactoring ×1
static ×1
syntax-error ×1
warnings ×1