。Net:如何在类中查找未使用的Fields / DataMember?

问题描述 投票:1回答:1

我有10个以上的DataMember,每个中都有150个以上的Datacontact。如何在代码中找到未使用的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

屏幕:“在此处输入图像描述”

vb.net wcf datacontract ndepend
1个回答
0
投票

我已经删除了DataMember中的属性关键字,然后运行了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
© www.soinside.com 2019 - 2024. All rights reserved.