Skip to content

Instantly share code, notes, and snippets.

@phuf
Created November 7, 2023 20:37
Show Gist options
  • Save phuf/c7d9e9e2d72423d138954d85306dd2a4 to your computer and use it in GitHub Desktop.
Save phuf/c7d9e9e2d72423d138954d85306dd2a4 to your computer and use it in GitHub Desktop.
PowerShell cmdlet that returns .NET classes with at least one public field.
#Requires -Version 7
<#
.SYNOPSIS
Return list of classes declared within the specified assembly that have at
least one public field.
.DESCRIPTION
Return list of classes within an assembly that have at least one field.
The use case is discovering fields that really should be properties.
.EXAMPLE
./Get-ClassWithAtLeastOneField.ps1 -Path (Get-Item My.dll).FullName
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[String] $Path
)
Add-Type -Path $Path
$Assembly = [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.Location.EndsWith([IO.Path]::GetFileName($Path)) }
$Assembly.GetTypes() | Where-Object { $_.IsClass -and $_.GetFields().Length -gt 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment