functions/import/Export-PSFModuleClass.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
function Export-PSFModuleClass { <# .SYNOPSIS Exports a module-defined PowerShell class irrespective of how the module is being imported. .DESCRIPTION Exports a module-defined PowerShell class irrespective of how the module is being imported. This avoids having to worry about how the module is being imported. Please beware the risk of class-name-collisions however. .PARAMETER ClassType The types to publish. .EXAMPLE PS C:\> Export-PSFModuleClass -ClassType ([MyModule_MyClass]) Publishes the class MyModule_MyClass, making it available outside of your module. #> [CmdletBinding()] Param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [System.Type[]] $ClassType ) begin { $internalExecutionContext = [PSFramework.Utility.UtilityHost]::GetExecutionContextFromTLS() $topLevelSessionState = [PSFramework.Utility.UtilityHost]::GetPrivateProperty('TopLevelSessionState', $internalExecutionContext) $globalScope = [PSFramework.Utility.UtilityHost]::GetPrivateProperty('GlobalScope', $topLevelSessionState) $addMethod = $globalScope.GetType().GetMethod('AddType', [System.Reflection.BindingFlags]'Instance, NonPublic') } process { foreach ($typeObject in $ClassType) { $arguments = @($typeObject.Name, $typeObject) $addMethod.Invoke($globalScope, $arguments) } } } |