Skip to content

PowerShell class inheriting from generic base with custom type fails if in same pipe #20755

Open

Description

@agowa

Prerequisites

Steps to reproduce

Call below test.ps1 file using e.g. . ./test.ps1

test.ps1:

class CustomItemType {}

class CustomQueueType : System.Collections.Generic.Queue[CustomItemType] {
  CustomQueueType() : base() {}
}

WORKAROUND (yes it's ugly, but sadly the only way that I found that currently works):

class CustomItemType {}
. ([scriptblock]::Create(@"
  class CustomQueueType : System.Collections.Generic.Queue[CustomItemType] {
    #CustomQueueType() : base() {}
  }
"@))

Expected behavior

types getting properly defined and being usable

Actual behavior

PS> . ./test.ps1
OperationStopped: Specified method is not supported.

Error details

PS> Get-Error

Exception             : 
    Type       : System.NotSupportedException
    TargetSite : 
        Name          : GetConstructorImpl
        DeclaringType : System.Reflection.Emit.TypeBuilderInstantiation, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : Specified method is not supported.
    Data       : System.Collections.ListDictionaryInternal
    Source     : System.Private.CoreLib
    HResult    : -2146233067
    StackTrace : 
   at System.Reflection.Emit.TypeBuilderInstantiation.GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
   at System.Type.GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
   at System.Management.Automation.PSTypeExtensions.HasDefaultCtor(Type type)
   at System.Management.Automation.Language.TypeDefiner.DefineTypeHelper.GetBaseTypes(Parser parser, TypeDefinitionAst typeDefinitionAst, List`1& interfaces)
   at System.Management.Automation.Language.TypeDefiner.DefineTypeHelper..ctor(Parser parser, ModuleBuilder module, TypeDefinitionAst typeDefinitionAst, String typeName)
   at System.Management.Automation.Language.TypeDefiner.DefineTypes(Parser parser, Ast rootAst, TypeDefinitionAst[] typeDefinitions)
   at System.Management.Automation.Language.Compiler.DefinePowerShellTypes(Ast rootForDefiningTypes, TypeDefinitionAst[] typeAsts)
   at System.Management.Automation.Language.Compiler.GenerateTypesAndUsings(ScriptBlockAst rootForDefiningTypesAndUsings, List`1 exprs)
   at System.Management.Automation.Language.Compiler.CompileSingleLambda(ReadOnlyCollection`1 statements, ReadOnlyCollection`1 traps, String funcName, IScriptExtent entryExtent, IScriptExtent exitExtent, ScriptBlockAst rootForDefiningTypesAndUsings)
   at System.Management.Automation.Language.Compiler.CompileNamedBlock(NamedBlockAst namedBlockAst, String funcName, ScriptBlockAst rootForDefiningTypes)
   at System.Management.Automation.Language.Compiler.VisitScriptBlock(ScriptBlockAst scriptBlockAst)
   at System.Management.Automation.Language.Compiler.Compile(CompiledScriptBlockData scriptBlock, Boolean optimize)
   at System.Management.Automation.CompiledScriptBlockData.ReallyCompile(Boolean optimize)
   at System.Management.Automation.CompiledScriptBlockData.CompileUnoptimized()
   at System.Management.Automation.CompiledScriptBlockData.Compile(Boolean optimized)
   at System.Management.Automation.DlrScriptCommandProcessor.Init()
   at System.Management.Automation.CommandDiscovery.CreateCommandProcessorForScript(ExternalScriptInfo scriptInfo, ExecutionContext context, Boolean useNewScope, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
   at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
   at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : OperationStopped: (:) [], NotSupportedException
FullyQualifiedErrorId : System.NotSupportedException
InvocationInfo        : 
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 3
    Line             : . .\test.ps1
    Statement        : . .\test.ps1
    PositionMessage  : At line:1 char:1
                       + . .\test.ps1
                       + ~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 22, 2023
  2. mklement0 commented on Nov 22, 2023

    @mklement0
    Contributor

    Yes, it is an unfortunate limitation, previously discussed in:

  3. agowa commented on Nov 22, 2023

    @agowa
    Author

    But creating the value types isn't the issue here, am I overlooking something in the ticket you referenced?
    Creating the value type works fine IF it is done AFTER the type it is depending on has been created manually.

    This more or less looks like some kind of dependency cycle issue and therefore unrelated/distinct from the referenced issue.
    If it wasn't possible at all, the Workaround would also not work.

  4. mklement0 commented on Nov 22, 2023

    @mklement0
    Contributor

    The title of the linked issue is somewhat misleading (so it's good to have yours as a signpost), but at heart it is the same issue:

    The inability to reference a custom class in the same script in the inheritance specifications of the same / another class:

    • The linked case: in the class' own interface specification; e.g.:
      class ServerName : System.IEquatable[ServerName] { } breaks, but
      class ServerName : System.IEquatable[object] { } works.
    • Your case: in another class' generic base-class specification.

    As for the workaround: Note that you can simply use Invoke-Expression.

    For the meta issue that aims to collect all class-related problem, see:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions