C #: Automatic removal of unnecessary assembly references?

I work with a large code base with a huge number of projects, each of which has a small (and in some cases a huge) number of links to others. Over time, significant refactoring has been done on this code base, and as a result there are many assemblies referenced by some projects just because they contained a class that has since been moved elsewhere; that kind of thing.

ReSharper has an integrated tool integrated into the IDE that allows users to find the code that actually uses this link for a given project, however, to turn this into a solution, we would have to force a person to right-click on each link in each project, and then actually check the absence of customs, and then remove them, which is not only a lengthy process, but also borders on torture.

I would like to be able to automate this process so that we simply run it and remove unnecessary links; we could then integrate it into some kind of regular process so that missed errors are caught.

Two options that I was thinking about would be: A) Automate ReSharper using Powershell, if possible, or B) perhaps the Visual Studio 2010 Architecture dependency diagrams can handle this and, possibly, the script, if I'm lucky.

My questions are as follows:

  • Could ReSharper be a script for something like that?
  • Does the VS2010 architecture provide the removal of unused links in any way with batch / automated access?
+3
source share
3 answers

You should be able to do this with a simple shell:

1) load a visual studio with your solution

2) put together a complete solution

3) leave VS and run powershell.exe

4) VS DTE ROT ( - , - , powershell ):

ps> $dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("visualstudio.dte")

5) , :

ps> $dte.solution.projects | select @{l="name";e={$_.name}}, `
        @{l="references";e={$_.object.references|select -exp name}} | ft -auto

... ...

6) script,

7), bin \, :

$assembly = [reflection.assembly]::reflectiononlyload($dll)

8)

$refs = $assembly.getreferencedassemblies()

9) , , VS DTE

# example
$currentproj.object.references.item("system.core").remove()
$currentproj.save()

10) !

, .net , . , , , .

-Oisin

+6

, csproj, bin PowerShell. .

, Oisin. - csproj ( xml ).

;)

+1

@x0n, . , - . , dll ReflectionOnlyLoad - COM. LoadFile. , LoadFile, , Reflector. PowerShell script , , , . "" . , . , - 4 "" . powershell script, "System". , "using System;", but before that it doesn’t work, saying that I need to reference this assembly because of the interface ... BTW, our project is not a toy, it includes 140 + dll (almost half of them are Test nUnitdll, though) , and I thought I would do some work. My script (which does not go recursively into project folders - some projects contain more dlls), so maybe someone can use it:

`
  $ dte = [System.Runtime.InteropServices.Marshal] :: GetActiveObject (" visualstudio.dte ")

$binfiles=Get-ChildItem C:\YourSourcePath\bin\debug
$dlls=$binfiles | where { $_.extension -eq ".dll" -and $_.name -like "*YourCompanyName*" }

foreach ($dll in $dlls) {
    foreach($prj in $dte.solution.projects) {
        if ($prj.Kind -eq "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") {
            if ($prj.Properties.Item("OutputFileName").Value -eq $dll.Name) {
                $loaded = [reflection.assembly]::LoadFile($dll.FullName)
                $refs = $loaded.GetReferencedAssemblies()
                Write "============="
                Write $dll.Name
                Write "-------------"
                foreach($pref in $prj.Object.References) {
                    $found = 0
                    foreach($ref in $refs) {
                        if ($ref.Name -eq $pref.Name) {
                            $found = 1
                            break;
                        }
                    }
                    if ($found -eq 0) {
                        Write $pref.Name
                    }
                }
            }
        }
    }
}

`

+1
source

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


All Articles