【发布时间】: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