From d0a91470c9c24d95f15bc8f4de199af58c59ca3c Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 15 Sep 2022 13:34:09 -0700 Subject: [PATCH] Filter out compiler generated types for `Add-Type -PassThru` (#18095) --- .../commands/utility/AddType.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 6cdfb512d..00d6b11ef 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -13,6 +13,7 @@ using System.Management.Automation; using System.Management.Automation.Internal; using System.Management.Automation.Security; using System.Reflection; +using System.Runtime.CompilerServices; using System.Runtime.Loader; using System.Security; using System.Text; @@ -891,7 +892,14 @@ namespace Microsoft.PowerShell.Commands private void WriteTypes(Assembly assembly) { - WriteObject(assembly.GetTypes(), true); + foreach (Type type in assembly.GetTypes()) + { + // We only write out types that are not auto-generated by compiler. + if (type.GetCustomAttribute() is null) + { + WriteObject(type); + } + } } #endregion LoadAssembly