Inspect .NET libraries from PowerShell: view C# source, find types, list references, and review library upgrades.
ISpy pairs with TextMate, my PowerShell module for syntax highlighting. Use them together to read decompiled C# with highlighting:
[Math]::Truncate | Expand-Type | Format-TextMateInstall-Module ISpy
Import-Module ISpyRequires PowerShell 7.6 or newer, or Windows PowerShell 5.1.
Pipe a method group, type, or cmdlet to Expand-Type:
[Math]::Truncate | Expand-Type
Get-Command Get-ChildItem | Expand-Type
[System.Management.Automation.PSObject] | Expand-TypeTo inspect a library file, find a type and expand it:
Get-Type -Path ./Library.dll -NamePattern '*Service' | Expand-Type
Expand-Type -Path ./Library.dll -TypeName MyLibrary.Service -MethodName ReadUse -Metadata to capture an object with source and method details:
$result = [Math]::Round | Expand-Type -Metadata
$result.MethodNames
$result.SourceCompare public and protected APIs between two versions of the same library:
$changes = Compare-Assembly ./old/Library.dll ./new/Library.dll
$changes
$changes.DifferencesAdd -IncludeSource for method-body changes and git-style source diffs. Add -IncludeAll to inspect private and internal declarations. Generated implementation types are excluded from the default public API view.
For a compatibility assessment, use Test-AssemblyCompatibility:
$report = Test-AssemblyCompatibility ./old/Library.dll ./new/Library.dll -ErrorAction Stop
$report.Findings
$report.Findings.ChangesHasBreakingChanges identifies detected breaking changes. RequiresReview identifies changes needing closer inspection. Neither result replaces testing your application. Compare assemblies built for the same target framework and keep their required dependencies available.
You can use the report in a dependency-update job:
$report = Test-AssemblyCompatibility ./old/Library.dll ./new/Library.dll -ErrorAction Stop
$report | ConvertTo-Json -Depth 50 -EnumsAsStrings | Set-Content ./compatibility.json
if ($report.HasBreakingChanges -or $report.RequiresReview) {
throw 'Library update needs review. See compatibility.json.'
}Get-Item ./Library.dll | Get-AssemblyInfo
Get-Item ./Library.dll | Get-Dependency
Get-Module ISpy | Get-Type -PublicOnlyAssembly commands also accept reflection assemblies, types, binary cmdlets/modules, and ISpy results that identify an assembly file. Get-Dependency lists declared references; those dependencies do not need to be installed.
Get-DecompiledSource ./Library.dll -TypeName MyLibrary.Service |
Select-Object -ExpandProperty Source |
Set-Content ./Service.cs
Export-DecompiledSource ./Library.dll ./source -CreateNamespaceDirectories# Settings and formatting
$formatSplat = @{
# Stroustrup-ish formatting
ClassBraceStyle = 'EndOfLine'
IndentationString = ' '
MethodBraceStyle = 'EndOfLine'
NewLineAferIndexerOpenBracket = 'NewLine'
StatementBraceStyle = 'EndOfLine'
ChainedMethodCallWrapping = 'WrapAlways'
InterfaceBraceStyle = 'EndOfLine'
StructBraceStyle = 'EndOfLine'
EnumBraceStyle = 'EndOfLine'
ConstructorBraceStyle = 'EndOfLine'
}
$options = @{
FileScopedNamespaces = $true
AlwaysUseBraces = $true
CSharpFormattingOptions = New-DecompilerFormattingOption @formatSplat
}
$settings = New-DecompilerSetting @options
[System.Console]::Write | Expand-Type -Settings $settingsCustom settings use standard decompiler formatting instead of the concise Expand-Type default. To use the decompiler's defaults without further customization:
[System.Console]::Write | Expand-Type -Settings (New-DecompilerSetting)
# Add this to your profile to use these settings for every Expand-Type call.
$PSDefaultParameterValues['Expand-Type:Settings'] = New-DecompilerSettingFor syntax highlighting, optionally pipe source text to Format-TextMate from TextMate.
Use Get-Help <command> -Examples for workflows or Get-Help <command> -Full for parameters.
| Command | Purpose |
|---|---|
| Compare-Assembly | Show API and optional source changes |
| Test-AssemblyCompatibility | Assess an update for breaking changes |
| Expand-Type | Inspect source for types, methods, and commands |
| Get-Type | Find and filter types |
| Get-AssemblyInfo | Show version, framework, and assembly details |
| Get-Dependency | List declared assembly references |
| Get-DecompiledSource | Return source objects for types |
| Export-DecompiledSource | Write C# files to a directory |
| New-DecompilerSetting | Choose syntax preferences |
| New-DecompilerFormattingOption | Choose indentation, spacing, and layout |
| New-Decompiler | Create a reusable decompiler |
See CONTRIBUTING.md for building, testing, and maintaining ISpy.