我每次拥有超过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
屏幕:
答案 0 :(得分:0)
我删除了DataMember中的属性关键字。然后我运行了Ndepend。 我能找到未使用的Datamembers。
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