static void Main(string[] args) { var analyzer = new DeadCodeAnalyzer(); analyzer.AssembliesToAnalyze.Add(typeof(Class1).Assembly); analyzer.EntryPoints.Add(typeof(Class1).GetMethod(nameof(Class1.Called))); var report = analyzer.Analyze(); }
static void Main(string[] args) { // you can use the standard AssemblyResolve on AppDomain to customize where to load assemblies from AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; var analyzer = new DeadCodeAnalyzer(); analyzer.Plugins.Add(new IgnoreJetBrainsPluigin()); analyzer.Plugins.Add(new AutofacConstructorPlugin()); // define which assemblies are yours (you probably don't care about dependencies) analyzer.AssembliesToAnalyze = Directory.GetFiles(BuildDir, "*.dll", SearchOption.AllDirectories) .Where(f => !f.Contains("Tests")) // remember to exclude test assemblies .Select(f => Assembly.LoadFile(f)) .ToList(); // define which code is called implicitly analyzer.EntryPoints = analyzer.AssembliesToAnalyze .SelectMany(a => a.GetTypes()) .SelectMany(t => FindEntryPoints(t)) .Where(mi => mi != null) .ToList(); // dynamically mark types are used implictily analyzer.MarkMethodsAsImplicitlyCalled = typeSeen => { if (typeSeen.Name == "MySpecialType") { // if you want to implicitly mark some methods on a type called, because of reflection code // but you only want to mark them if the class is seen then you can do that here return(new List <MethodBase> { /* return implicitly called methods here */ }); } return(null); }; // this is where all of the magic happens var report = analyzer.Analyze(); // after analysis you can further filter the results before outputting anything var namespacesToIgnore = new[] { "" }; // for example, there are often namespaces that you just want to ignore completely var unusedClassesFiltered = report.UnusedClasses .OrderBy(uc => uc.Namespace) .Where(uc => uc.IgnoreProbableConstClasses() && namespacesToIgnore.Any(badns => uc.Namespace?.Contains(badns) ?? false) == false) .Select(uc => uc.FullName) .ToList(); // since the unused methods and classes are returned as Type and MethodBase objects you can decide how to format them too FormatOutputForFile(report, unusedClassesFiltered); Console.WriteLine("Done!"); Console.ReadKey(); }
public void HappyPathMvcSite() { //+Arrange var allMethods = GetMethodsFromAssembly(typeof(HomeController).Assembly); var analyzer = new DeadCodeAnalyzer(); analyzer.AssembliesToAnalyze.Add(typeof(HomeController).Assembly); analyzer.EntryPoints.Add(typeof(HomeController).GetMethod(nameof(HomeController.Index))); //+Act var report = analyzer.Analyze(); //+Assert var output = FormatVerify(allMethods, report); Approvals.Verify(output); }
public void HappyPath() { //+Arrange var allMethods = GetMethodsFromAssembly(typeof(Class1).Assembly); var analyzer = new DeadCodeAnalyzer(); analyzer.AssembliesToAnalyze.Add(typeof(Class1).Assembly); analyzer.EntryPoints.Add(typeof(Class1).GetMethod(nameof(Class1.Called))); //+Act var report = analyzer.Analyze(); //+Assert string output = FormatVerify(allMethods, report); Approvals.Verify(output); }
protected string GetJavascriptWithDCE(string fileName, string expectedText = null) { var dce = new DeadCodeAnalyzer(); var analyzerSettings = new Dictionary <string, object> { { "DeadCodeElimination", true }, { "NonAggressiveVirtualMethodElimination", false }, { "WhiteList", new List <string> { @"System\.Void Program::Main\(.*\)" } } }; dce.SetConfiguration(analyzerSettings); var defaultConfiguration = MakeConfiguration(); var generatedJs = GetJavascript( Path.Combine(@"..\Tests.DCE", fileName), expectedText, makeConfiguration: () => defaultConfiguration, analyzers: new[] { dce } ); return(generatedJs); }