Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. function New-DynamicModuleBuilder {
  2. <#
  3. .SYNOPSIS
  4. Creates a new assembly and a dynamic module within the current AppDomain.
  5. .DESCRIPTION
  6. 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.
  7. .EXAMPLE
  8. New-DynamicModuleBuilder
  9. #>
  10.  
  11. [CmdletBinding()]
  12. [OutputType([System.Reflection.Emit.ModuleBuilder])]
  13. param (
  14. # A name for the in-memory assembly.
  15. [Parameter(Mandatory = $true)]
  16. [AssemblyName]$AssemblyName = (New-Guid).ToString(),
  17.  
  18. # 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.
  19. [String]$DynamicAssemblyVariable = 'DefaultDynamicAssembly',
  20.  
  21. #
  22. [Switch]$PassThru
  23. )
  24.  
  25. try {
  26. $appDomain = [AppDomain]::CurrentDomain
  27.  
  28. # Create a dynamic assembly in the current AppDomain
  29. $assemblyBuilder = $appDomain.DefineDynamicAssembly(
  30. $AssemblyName,
  31. [AssemblyBuilderAccess]::Run
  32. )
  33.  
  34. $moduleBuilder = $assemblyBuilder.DefineDynamicModule($AssemblyName.Name)
  35. # Create a transient dynamic module within the new assembly
  36. New-Variable $DynamicAssemblyVariable -Scope Global -Value $moduleBuilder
  37. if ($PassThru) {
  38. $moduleBuilder
  39. }
  40. } catch {
  41. throw
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement