Skip to content

Instantly share code, notes, and snippets.

@SeeminglyScience
Created August 14, 2019 20:55
Show Gist options
  • Save SeeminglyScience/1ba55937bd296276a976723c39c23179 to your computer and use it in GitHub Desktop.
Save SeeminglyScience/1ba55937bd296276a976723c39c23179 to your computer and use it in GitHub Desktop.
function Expand-MemberInfo {
[Alias('emi')]
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline, Mandatory)]
[Alias('Member')]
[psobject] $InputObject,
[Parameter()]
[ValidateSet('IL', 'CSharp', 'VisualBasic')]
[string] $Language = 'CSharp',
[switch] $NoAnonymousMethods,
[switch] $NoExpressionTrees,
[switch] $NoYield,
[switch] $NoAsync,
[switch] $NoAutomaticProperties,
[switch] $NoAutomaticEvents,
[switch] $NoUsingStatements,
[switch] $NoForEachStatements,
[switch] $NoLockStatements,
[switch] $NoSwitchOnString,
[switch] $NoUsingDeclarations,
[switch] $NoQueryExpressions,
[switch] $DontClarifySameNameTypes,
[switch] $UseFullnamespace,
[switch] $DontUseVariableNamesFromSymbols,
[switch] $NoObjectOrCollectionInitializers,
[switch] $NoInlineXmlDocumentation,
[switch] $DontRemoveEmptyDefaultConstructors,
[switch] $DontUseIncrementOperators,
[switch] $DontUseAssignmentExpressions,
[switch] $AlwaysCreateExceptionVariables,
[switch] $SortMembers,
[switch] $ShowTokens,
[switch] $ShowBytes,
[switch] $ShowPdbInfo
)
begin {
$dnSpy = Get-Command -CommandType Application -Name dnSpy.Console.exe -ErrorAction Stop
$argumentList = & {
if ($NoAnonymousMethods.IsPresent) {
'--no-anon-methods'
}
if ($NoExpressionTrees.IsPresent) {
'--no-expr-trees'
}
if ($NoYield.IsPresent) {
'--no-yield'
}
if ($NoAsync.IsPresent) {
'--no-async'
}
if ($NoAutomaticProperties.IsPresent) {
'--no-auto-props'
}
if ($NoAutomaticEvents.IsPresent) {
'--no-auto-events'
}
if ($NoUsingStatements.IsPresent) {
'--no-using-stmt'
}
if ($NoForEachStatements.IsPresent) {
'--no-foreach-stmt'
}
if ($NoLockStatements.IsPresent) {
'--no-lock-stmt'
}
if ($NoSwitchOnString.IsPresent) {
'--no-switch-string'
}
if ($NoUsingDeclarations.IsPresent) {
'--no-using-decl'
}
if ($NoQueryExpressions.IsPresent) {
'--no-query-expr'
}
if ($DontClarifySameNameTypes.IsPresent) {
'--no-ambig-full-names'
}
if ($UseFullnamespace.IsPresent) {
'--full-names'
}
if ($DontUseVariableNamesFromSymbols.IsPresent) {
'--use-debug-syms'
}
if ($NoObjectOrCollectionInitializers.IsPresent) {
'--no-obj-inits'
}
if ($NoInlineXmlDocumentation.IsPresent) {
'--no-xml-doc'
}
if ($DontRemoveEmptyDefaultConstructors.IsPresent) {
'--dont-remove-empty-ctors'
}
if ($DontUseIncrementOperators.IsPresent) {
'--no-inc-dec'
}
if ($DontUseAssignmentExpressions.IsPresent) {
'--dont-make-assign-expr'
}
if ($AlwaysCreateExceptionVariables.IsPresent) {
'--always-create-ex-var'
}
if ($SortMembers.IsPresent) {
'--sort-members'
}
if ($ShowBytes.IsPresent) {
'--bytes'
}
if ($ShowPdbInfo.IsPresent) {
'--pdb-info'
}
if ($Language -ne 'CSharp') {
$languageGuid = switch ($Language) {
IL { '{a4f35508-691f-4bd0-b74d-d5d5d1d0e8e6}' }
CSharp { '{bba40092-76b2-4184-8e81-0f1e3ed14e72}' }
VisualBasic { '{a4f35508-691f-4bd0-b74d-d5d5d1d0e8e6}' }
}
"-l ""$languageGuid"""
}
}
if ($argumentList.Count -gt 1) {
$arguments = $argumentList -join ' '
return
}
$arguments = [string]$argumentList
}
process {
if ($InputObject -is [PSMethod]) {
$null = $PSBoundParameters.Remove('InputObject')
return $InputObject.ReflectionInfo | & $MyInvocation.MyCommand @PSBoundParameters
}
if ($InputObject -is [type]) {
$assembly = $InputObject.Assembly
} else {
$assembly = $InputObject.DeclaringType.Assembly
}
$sb = [StringBuilder]::new($arguments)
if ($sb.Length -gt 0) {
$null = $sb.Append(' ')
}
if (-not $ShowTokens.IsPresent) {
$null = $sb.Append('--no-tokens ')
}
try {
# Use the special name accessor as PowerShell ignores property exceptions.
$metadataToken = $InputObject.get_MetadataToken()
} catch [InvalidOperationException] {
$exception = [PSArgumentException]::new(
('Unable to get the metadata token of member "{0}". Ensure ' -f $InputObject) +
'the target is not dynamically generated and then try the command again.',
$PSItem)
$PSCmdlet.WriteError(
[ErrorRecord]::new(
<# exception: #> $exception,
<# errorId: #> 'CannotGetMetadataToken',
<# errorCategory: #> [ErrorCategory]::InvalidArgument,
<# targetObject: #> $InputObject))
return
}
$null = $sb.
AppendFormat('--md {0} ', $metadataToken).
AppendFormat('"{0}"', $assembly.Location)
& ([scriptblock]::Create(('& "{0}" {1}' -f $dnSpy.Source, $sb.ToString())))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment