Static .net assembly analysis

I have a C # project for which I need to find all private methods that are not called from any other public method directly or indirectly.

In addition, for each private method that is called from an open method, I need to know what public method it is. Then I will evaluate whether this method is really called from the client of the class, and if not, I can delete it.

In the past, I used code from Lutz Rorder, which is the Reflector base - it was able to parse IL code and give an object model on top of it. I can not find this code now.

Any suggestion? Maybe the point in this Lutz Rorder code?

Saar

+3
source share
6 answers

You should check out Nitriq Static Code Analysis for .Net - They have a free community version and their full license version is pretty reasonable.

+3
source

As Thomas pointed out, the NDepend tool can help you find unused code in your .NET database. Disclaimer: I am the developer of this tool.

NDepend suggests writing a Code Rule over a LINQ query (CQLinq) . About 200 default code rules , 3 of which are dedicated to detecting unused / dead code:

  • Potentially dead types (therefore, detect an unused class, structure, interface, delegate ...)
  • ( , ctor, getter/setter...)

NDepend Visual Studio, // IDE. CI, reports, .

3 , , , , . , , , ().

, . , , .

3 . , , , / , . , , .


, , , , .

, CQLinq :

from m in Application.Methods
where m.IsPrivate
let publicMethodsCallingMe = m.MethodsCallingMe.Where(m1 => m1.IsPublic)
where publicMethodsCallingMe.Count() > 0
select new { m, publicMethodsCallingMe }

:

Browsing CQLinq query result

+3

, ( , , ...) ( ) , NDepend.

, SQL- , . CQL, , NDepend IntelliSense/, / .

(AFAIK , ...)

!

+2

, , FXCop . # .

FXCop (MSDN)

+1

. "". .

, "".

, , SDK Reflector.

0

, PowerCommands for Reflector addin (http://powercommands.codeplex.com). , , .

CCI Cecil, .

Query:

from a in AssemblyManager.Assemblies.Cast<IAssembly>()
where a.Name != "mscorlib" 
    && !a.Name.Contains("System")
from m in a.Modules.Cast<IModule>()
from t in m.Types.Cast<ITypeDeclaration>()
from mt in t.Methods.Cast<IMethodDeclaration>()
where mt.Visibility == MethodVisibility.Public 
    && !mt.RuntimeSpecialName 
    && !mt.SpecialName 
    && mt.Body is IMethodBody
from i in ((IMethodBody)mt.Body).Instructions.Cast<IInstruction>()
where i != null 
    && i.Value != null 
    && i.Value is IMethodReference 
    && ((IMethodReference)i.Value).Resolve() != null
    && ((IMethodReference)i.Value).Resolve().Visibility == MethodVisibility.Private
select new { 
    CallingMethod=t.Namespace + "." + t.Name + "." + mt.Name, 
    PrivateReferencedMethod=((ITypeReference)((IMemberReference)((IMethodReference)i.Value).Resolve()).DeclaringType).Namespace + "."
        + ((ITypeReference)((IMemberReference)((IMethodReference)i.Value).Resolve()).DeclaringType).Name + "."
        + ((IMethodReference)i.Value).ToString()
}
0

Source: https://habr.com/ru/post/1721857/


All Articles