Skip to content
trackdPublic

About

A PowerShell module for decompiling .NET assemblies using the ILSpy decompiler engine.

Resources

Contributing

Stars

12 stars

Watchers

1 watching

Forks

Repository files navigation

ISpy

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

Install

Install-Module ISpy
Import-Module ISpy

Requires PowerShell 7.6 or newer, or Windows PowerShell 5.1.

Inspect source

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

To 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 Read

Use -Metadata to capture an object with source and method details:

$result = [Math]::Round | Expand-Type -Metadata
$result.MethodNames
$result.Source

Review a library upgrade

Compare public and protected APIs between two versions of the same library:

$changes = Compare-Assembly ./old/Library.dll ./new/Library.dll
$changes
$changes.Differences

Add -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.Changes

HasBreakingChanges 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.'
}

List assembly details and references

Get-Item ./Library.dll | Get-AssemblyInfo
Get-Item ./Library.dll | Get-Dependency
Get-Module ISpy | Get-Type -PublicOnly

Assembly 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.

Save source

Get-DecompiledSource ./Library.dll -TypeName MyLibrary.Service |
    Select-Object -ExpandProperty Source |
    Set-Content ./Service.cs

Export-DecompiledSource ./Library.dll ./source -CreateNamespaceDirectories

Choose source formatting

# 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 $settings

Custom 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-DecompilerSetting

For syntax highlighting, optionally pipe source text to Format-TextMate from TextMate.

Command help

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

Contributing

See CONTRIBUTING.md for building, testing, and maintaining ISpy.

License

MIT. Decompilation uses ILSpy.

About

A PowerShell module for decompiling .NET assemblies using the ILSpy decompiler engine.

Resources

Contributing

Stars

12 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages