Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function New-DynamicModuleBuilder {
- <#
- .SYNOPSIS
- Creates a new assembly and a dynamic module within the current AppDomain.
- .DESCRIPTION
- Prepares a System.Reflection.Emit.ModuleBuilder class to allow construction of dynamic types. The ModuleBuilder is created to allow the creation of multiple types under a single assembly.
- .EXAMPLE
- New-DynamicModuleBuilder
- #>
- [CmdletBinding()]
- [OutputType([System.Reflection.Emit.ModuleBuilder])]
- param (
- # A name for the in-memory assembly.
- [Parameter(Mandatory = $true)]
- [AssemblyName]$AssemblyName = (New-Guid).ToString(),
- # By default, this function stores a ModuleBuilder in a global variable called DefaultDynamicAssembly. The ModuleBuilder object is available for New-Enum without needing explicit assignment.
- [String]$DynamicAssemblyVariable = 'DefaultDynamicAssembly',
- #
- [Switch]$PassThru
- )
- try {
- $appDomain = [AppDomain]::CurrentDomain
- # Create a dynamic assembly in the current AppDomain
- $assemblyBuilder = $appDomain.DefineDynamicAssembly(
- $AssemblyName,
- [AssemblyBuilderAccess]::Run
- )
- $moduleBuilder = $assemblyBuilder.DefineDynamicModule($AssemblyName.Name)
- # Create a transient dynamic module within the new assembly
- New-Variable $DynamicAssemblyVariable -Scope Global -Value $moduleBuilder
- if ($PassThru) {
- $moduleBuilder
- }
- } catch {
- throw
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement