NDepend CQL查询缺少IDisposable实现 [英] NDepend CQL Query for missing IDisposable implementation

查看:101
本文介绍了NDepend CQL查询缺少IDisposable实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到此问题正在寻找的查询不足以找到IDisposable实现的每个小问题,但是每个预警都很重要,因此我将尽我所能.

I realize that the query this question is looking for won't be enough to find every little problem with IDisposable implementations, but every early warning counts, so I'll take what I can get.

我想知道是否有人提出了NDepend的CQL查询,该查询将列出所有未实现IDisposable的类,但具有一个或多个可以实现IDisposable的类.一个类可能通过错误(例如,有人忘记检查IDisposable实现的字段类型)或通过代码演变(例如,某个字段中使用了IDisposable的字段中使用的类)最终出现在此查询的结果列表上以后的日期,而不会更新所有用法).

I'd like to know if anyone has come up with a CQL query for NDepend that will list all classes that doesn't implement IDisposable, but has one or more fields that does. A class could end up on the resultlist of this query either through a bug (ie. someone forgot to check the field types for IDisposable implementations), or through code evolution (ie. a class used in a field somewhere gets IDisposable tacked on at a later date without all usages being updated).

查找所有未实现IDisposable的类的简单查询是:

The simple query to find all classes that doesn't implement IDisposable is:

SELECT TYPES WHERE !Implement "System.IDisposable"

但是,这当然不会检查类是否应该为上述规则实现IDisposable.

However, this will of course not check if the class should implement IDisposable for the above rule.

有人有这样的查询吗?我仍然对CQL有所了解,所以这部分让我望而却步.

Does anyone have such a query? I'm still getting to grips with CQL so this part eludes me.

推荐答案

Lasse,由于有了CQLinq(基于LINQ的代码规则)功能,现在可以实现应实现IDisposable的类型匹配.实际上,现在提供了两个相关的默认规则,您可以轻松编写自己的相关规则:

Lasse, thanks to CQLinq (Code Rule over LINQ) capabilities matching types that should implement IDisposable is now possible. Actually two related default rules are now provided, and you can easily write your own related rules:

  • Types with disposable instance fields must be disposable
  • Disposable types with unmanaged resources should declare finalizer
// <Name>Types with disposable instance fields must be disposable</Name>
warnif count > 0

let iDisposable = ThirdParty.Types.WithFullName("System.IDisposable").FirstOrDefault() 
where iDisposable != null // iDisposable can be null if the code base doesn't use at all System.IDisposable

from t in Application.Types where 
   !t.Implement(iDisposable) && 
   !t.IsGeneratedByCompiler 

let instanceFieldsDisposable = 
    t.InstanceFields.Where(f => f.FieldType != null &&
                                f.FieldType.Implement(iDisposable))

where instanceFieldsDisposable.Count() > 0
select new { t, instanceFieldsDisposable }


// <Name>Disposable types with unmanaged resources should declare finalizer</Name>
// warnif count > 0
let iDisposable = ThirdParty.Types.WithFullName("System.IDisposable").SingleOrDefault()
where iDisposable != null // iDisposable can be null if the code base deosn't use at all System.IDisposable

let disposableTypes = Application.Types.ThatImplement(iDisposable)
let unmanagedResourcesFields = disposableTypes.ChildFields().Where(f => 
   !f.IsStatic && 
    f.FieldType != null && 
    f.FieldType.FullName.EqualsAny("System.IntPtr","System.UIntPtr","System.Runtime.InteropServices.HandleRef")).ToHashSet()
let disposableTypesWithUnmanagedResource = unmanagedResourcesFields.ParentTypes()

from t in disposableTypesWithUnmanagedResource
where !t.HasFinalizer
let unmanagedResourcesTypeFields = unmanagedResourcesFields.Intersect(t.InstanceFields)
select new { t, unmanagedResourcesTypeFields }

这篇关于NDepend CQL查询缺少IDisposable实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆