sách gpt4 ai đã đi

idisposable - NDepend CQL 查询缺少 IDisposable 实现

In lại 作者:行者123 更新时间:2023-12-05 00:44:13 hai mươi bốn 4
mua khóa gpt4 Nike

我意识到这个问题正在寻找的查询不足以找到 IDisposable 实现的每一个小问题,但每个早期警告都很重要,所以我会尽我所能。

我想知道是否有人提出了 NDepend 的 CQL 查询,该查询将列出所有未实现 IDisposable 但具有一个或多个字段的类。一个类可能通过错误(即有人忘记检查 IDisposable 实现的字段类型)或通过代码演变(即,在某个字段中使用的类在某个时候被添加到 IDisposable稍后更新所有用法)。

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

SELECT TYPES WHERE !Implement "System.IDisposable"

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

有人有这样的疑问吗?我仍在掌握 CQL,所以这部分是我回避的。

1 Câu trả lời

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

  • Types with disposable instance fields must be disposable
  • Disposable types with unmanaged resources should declare finalizer

  • // Types with disposable instance fields must be disposable
    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 }
    // Disposable types with unmanaged resources should declare finalizer
    // 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 }

    关于idisposable - NDepend CQL 查询缺少 IDisposable 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/312857/

    hai mươi bốn 4 0
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    Giấy chứng nhận ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com