Skip to content

Suggestion: Add #Requires -Assembly AssemblyName support #5022

Closed

Description

Currently, if I want to reference a .NET assembly like System.Configuration, I do something like this at the top of my scripts:

try{[void][Configuration.ConfigurationManager]}catch{Add-Type -AN System.Configuration}

it would be much more readable (and declarative) as something like:

#Requires -Assembly System.Configuration

Activity

  1. PetSerAl commented on Oct 5, 2017

    @PetSerAl
    Contributor

    Why do you put Add-Type in catch? You could also put using assembly System.Configuration on top of script.

  2. JasonG-STCU commented on Oct 5, 2017

    @JasonG-STCU

    I, too, have used this in a script of mine. It would absolutely be beneficial to be able to include it this way.

  3. brianary commented on Oct 5, 2017

    @brianary
    ContributorAuthor

    PetSerAl I wasn't aware of the using keyword, though I don't see assembly documented as an argument to it. Also, our servers are not uniformly on PowerShell 5 just yet, so that extended syntax isn't something we can fully rely on yet (my workstation is stuck on version 5.0.10105.0-April2015Preview which doesn't support using assembly, and I'm unable to upgrade or downgrade or uninstall it from Windows 7). It seems odd to have another thing that must come first in the script, along with [CmdletBinding()]Param(…). It looks like using must precede [CmdletBinding()]Param(…).

    The reason I use the above formulation is to Add-Type only if referencing the type causes an exception, that way I'm only adding it once per session.

  4. PetSerAl commented on Oct 6, 2017

    @PetSerAl
    Contributor

    The reason I use the above formulation is to Add-Type only if referencing the type causes an exception, that way I'm only adding it once per session.

    You can not load same assembly twice anyway. No harm on calling Add-Type for already loaded assembly.

    $a = Add-Type -AssemblyName System.Configuration -PassThru | Select-Object -First 1 -ExpandProperty Assembly
    $b = Add-Type -AssemblyName System.Configuration -PassThru | Select-Object -First 1 -ExpandProperty Assembly
    [Object]::ReferenceEquals($a, $b)
  5. brianary commented on Oct 6, 2017

    @brianary
    ContributorAuthor

    I'll have to look into the using syntax (I hope it gets added to the docs, and is officially supported) once I can finally upgrade things and get the new features.

    However, even on a newer machine, there's a pretty big performance difference:

    PS C:\Users\me> "$(Measure-Command {0..10000|% {iex 'using assembly System.Configuration'}})"
    00:00:08.2655849
    PS C:\Users\me> "$(Measure-Command {0..10000|% {Add-Type -AN System.Configuration}})"
    00:00:04.6386843
    PS C:\Users\me> "$(Measure-Command {0..10000|% {try{[void][Configuration.ConfigurationManager]}catch{Add-Type -AN System.Configuration}}})"
    00:00:00.0728870
    

    on my older machine, it's even bigger:

    PS C:\Users\me> "$(Measure-Command {0..10000|% {Add-Type -AN System.Configuration}})"
    00:00:08.1306933
    PS C:\Users\me> "$(Measure-Command {0..10000|% {try{[void][Configuration.ConfigurationManager]}catch{Add-Type -AN System.Configuration}}})"
    00:00:00.2983187
    
  6. added
    Issue-Question
    on Oct 6, 2017
  7. rjmholt commented on Mar 23, 2018

    @rjmholt
    Collaborator

    I've opened an RFC on updating the behaviour of #requires. Please feel free to comment in the PR.

  8. ghost closed this as completedon Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions