【问题标题】:.Net: How to find unused Fields / DataMember in the class?.Net:如何在课堂上找到未使用的 Fields / DataMember?
【发布时间】:2014-08-05 06:08:35
【问题描述】:

我有超过 150 个 Datacontacts,每个我有超过 10 个 DataMember。如何在代码中找到未使用的Datamember?

我可以通过右键单击 DataMember 和“查找所有引用”选项找到,但这不是我的问题的解决方案,因为我有大量的 DataMember。

更新: 我找到了一个 Vs2010 插件Ndepend。使用它我可以找到未使用的方法和类,但找不到 DataMember。 我在 Ndepend 中尝试过下面的代码,但它不起作用。

// <Name>Potentially dead Fields</Name>
warnif count > 0
from f in JustMyCode.Fields where   
f.NbMethodsUsingMe == 0 &&   
!f.IsPublic &&     // Although not recommended, public fields might be used by client applications of your assemblies.   
!f.IsLiteral &&    // The IL code never explicitely uses literal fields.   
!f.IsEnumValue &&  // The IL code never explicitely uses enumeration value.   
f.Name !=  "value__"  && // Field named 'value__' are relative to enumerations and the IL code never explicitely uses them.  
!f.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch()) &&   
!f.IsGeneratedByCompiler   // If you don't want to link NDepend.API.dll, you can use your own IsNotDeadCodeAttribute and adapt this rule.
select f

屏幕:

【问题讨论】:

  • 请准确地说DataMember 是什么意思?看起来你的意思是一个类的字段?如果是这样,“默认规则可能失效的字段不起作用”是什么意思?是什么让您认为这条规则不起作用?
  • @PatrickfromNDependteam,是的,我的意思是类的字段。我已经尝试上面的代码来查找死字段,但我得到的结果是“没有匹配的字段”。实际上我的代码中有死字段。
  • 因此,如果您有 (f.NbMethodsUsingMe == 0) 未使用的字段,那么调查查询中的哪个否定子句导致不匹配它们会很有趣。
  • @PatrickfromNDependteam 我已经编辑了我的问题。看看
  • 你的工具很棒......非常感谢@PatrickfromNDependteam

标签: vb.net wcf datacontract ndepend


【解决方案1】:

我已经删除了 DataMember 中的 Property Key word。然后我运行了 Ndepend。 我能够找到未使用的数据成员。

Public Class AccountInformation
    <DataMember()>
    Public Property AccountNumber As String
    <DataMember()>
    Public Property OtherElementQualifier As String
    <DataMember()>
    Public Property OtherElementNumber As String
End Class  

删除属性关键字

Public Class AccountInformation
    <DataMember()>
    Public AccountNumber As String
    <DataMember()>
    Public OtherElementQualifier As String
    <DataMember()>
    Public OtherElementNumber As String
End Class

然后在 Ndepend 中运行以下脚本。

// <Name>Potentially dead Fields</Name>
warnif count > 0
from f in JustMyCode.Fields where   
f.NbMethodsUsingMe == 0 &&   
//!f.IsPublic &&   Although not recommended, public fields might be used by client applications of your assemblies.   
!f.IsLiteral &&    // The IL code never explicitely uses literal fields.   
!f.IsEnumValue &&  // The IL code never explicitely uses enumeration value.   
f.Name !=  "value__"  && // Field named 'value__' are relative to enumerations and the IL code never explicitely uses them.  
!f.HasAttribute("NDepend.Attributes.IsNotDeadCodeAttribute".AllowNoMatch()) &&   
!f.IsGeneratedByCompiler   // If you don't want to link NDepend.API.dll, you can use your own IsNotDeadCodeAttribute and adapt this rule.
select f

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多