Skip to content

Improve Module Class Exports #539

Open
@StartAutomating

Description

@StartAutomating

Classes are currently a pain to export from a module in PowerShell.

This can be worked around, given this sort of code ($classScript would be a script that defines classes) :

$classDefs = $classScript.Ast.FindAll({param($ast) $ast -is [Management.Automation.Language.TypeDefinitionAst]},$true)
$accelerators = ([psobject].assembly.gettype("System.Management.Automation.TypeAccelerators"))
if ($classDefs) {
    . $classScript
    foreach ($classDef in $classDefs) {
        $null = $accelerators::Remove($classDefs.Name)
        $null = $accelerators::Add($classDef.Name, ($classDef.Name -as [type]))
    }
}

Using this workaround will surface the class via a type accelerator.

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions