Created
November 7, 2023 20:37
-
-
Save phuf/c7d9e9e2d72423d138954d85306dd2a4 to your computer and use it in GitHub Desktop.
PowerShell cmdlet that returns .NET classes with at least one public field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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