标签: resharper

虚拟成员在构造函数中调用

我从ReSharper收到一条关于从我的对象构造函数调用虚拟成员的警告.

为什么不做这件事?

c# resharper constructor warnings virtual-functions

1270
推荐指数
11
解决办法
17万
查看次数

方法可以做成静态,但应该吗?

Resharper喜欢指出每个asp.net页面可以静态化的多个函数.如果我让它们静止,对我有帮助吗?我应该将它们设置为静态并将它们移动到实用程序类吗?

.net c# resharper refactoring static-methods

356
推荐指数
10
解决办法
6万
查看次数

处理可能多次枚举IEnumerable的警告

在我的代码中需要使用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,然后避免可能的多次枚举,但后来我没有得到我能处理的最高对象.
  • 我可以做的另一件事是将转换IEnumerableList在方法的开头:

 public List<object> Foo(IEnumerable<object> objects)
 {
    var objectList = objects.ToList();
    // ...
 }
Run Code Online (Sandbox Code Playgroud)

但这只是尴尬.

在这种情况下你会做什么?

.net c# resharper performance

339
推荐指数
4
解决办法
12万
查看次数

如何在Visual Studio中禁用ReSharper并再次启用它?

我安装了ReSharper,它可以在Visual Studio中运行,但如何禁用它?

每当我在ReSharper菜单中搜索时,我都找不到禁用选项.

resharper visual-studio-2008 visual-studio

335
推荐指数
12
解决办法
15万
查看次数

访问Modified Closure

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抱怨这是"访问修改后的闭包",但上述情况似乎工作正常.任何人都可以阐明这一点吗?

(这个主题在这里继续)

c# resharper closures

310
推荐指数
2
解决办法
8万
查看次数

为什么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

c# linq resharper

291
推荐指数
3
解决办法
7万
查看次数

LINQ:不是所有人都不要

通常我想检查提供的值是否与列表中的值匹配(例如,在验证时):

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是否让我误入歧途?

.net c# linq resharper performance

261
推荐指数
8
解决办法
7万
查看次数

反转"if"语句以减少嵌套

当我在我的代码上运行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".

c# resharper

257
推荐指数
17
解决办法
11万
查看次数

ReSharper警告:"通用类型的静态字段"

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# generics resharper static asp.net-mvc-3

251
推荐指数
4
解决办法
4万
查看次数

即使项目构建,Visual Studio也会显示错误

我在C#解决方案上遇到Visual Studio问题.它显示完全随机的错误,但项目构建.现在,我有33个文件有错误,我可以在所有文件中看到红色的波浪线.

我尝试清理/重建解决方案,关闭Visual Studio甚至重新启动计算机.我还确保执行调试运行中描述的步骤,即使编译器在Visual Studio中有错误也是如此.我可以修改.cs文件,我看到解决方案中的变化.

有没有人知道它为什么这样做?

c# resharper intellisense syntax-error visual-studio

251
推荐指数
14
解决办法
8万
查看次数