Il semble que les fermetures PowerShell ne saisissent pas la définition des fonctions:Fonction de capture dans PowerShell Fermeture
PS C:\> function x() { Write-Host 'original x' }
PS C:\> function x-caller-generator() { return { Write-host 'Calling x!'; x }.GetNewClosure() }
PS C:\> $y = x-caller-generator
PS C:\> & $y
Calling x!
original x
PS C:\> function x() { Write-Host 'new x' }
PS C:\> & $y
Calling x!
new x
Est-il possible de saisir la définition d'une fonction?
Ce que je ressens réellement, c'est que je crée une fermeture, mais quand ma fermeture est exécutée, la fonction est hors de portée en quelque sorte. (Il est un peu étrange module psake pour les scripts de build est en train de faire.) Quelque chose comme ceci:
PS C:\> function closure-maker() {
>> function x() { Write-Host 'x!' }
>>
>> return { Write-host 'Calling x'; x }.GetNewClosure()
>> }
>>
PS C:\> $y = closure-maker
PS C:\> & $y
Calling x
The term 'x' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:39
+ return { Write-host 'Calling x'; x <<<< }.GetNewClosure()
+ CategoryInfo : ObjectNotFound: (x:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Remarque: L'utilisation PowerShell 2.0, mais intéressé par 3.0 réponses s'il y a quelque chose de nouveau.