Repository navigation
Open
Labels
Needs-TriageThe issue is new and needs to be triaged by a work group.
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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 usableActual 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 1Environment 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.0Visuals
No response
Metadata
Metadata
Assignees
Labels
Needs-TriageThe issue is new and needs to be triaged by a work group.
Activity
mklement0 commentedon Nov 22, 2023
Yes, it is an unfortunate limitation, previously discussed in:
agowa commentedon Nov 22, 2023
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.
mklement0 commentedon Nov 22, 2023
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
classin the same script in the inheritance specifications of the same / another class:class ServerName : System.IEquatable[ServerName] { }breaks, butclass ServerName : System.IEquatable[object] { }works.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: