vb.netwcfdatacontractndepend

.Net: How to find unused Fields / DataMember in the class?


I'm having more than 150 Datacontacts in each I've more than 10 DataMember. How to find the unused Datamember in the code?

I'm able to find by right-clicking on the DataMember and "Find All Reference" option, But this is not a solution to my problem because I’ve huge number of DataMember.

update : I've find one Vs2010 plug-in Ndepend.Using this I'm able to find unused methods and classes but not DataMember. I've tried below code in Ndepend but it is not working.

// <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

screen : enter image description here


Solution

  • I've removed the Property Key word in the DataMember.then I ran the Ndepend. I'm able to find the unused Datamembers.

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

    remove Property Keyword

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

    then run the below script in 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