Chris Hayes
Chris Hayes

Reputation: 4035

Complex objects in closures

I want to maintain state in a returned closure:

function HashMaker {

        $enc = [System.Text.Encoding]::UTF8
        $hasher = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider

        return {
            param($value)
            [System.BitConverter].ToString($hasher.ComputeHash($enc.GetBytes($value)))
        }.GetNewClosure()
}

There's obviously something wrong with this approach

Upvotes: 0

Views: 55

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

Don't call ToString() on the [BitConverter] type itself, call its static ToString() method:

return {
        param($value)
        [System.BitConverter]::ToString($hasher.ComputeHash($enc.GetBytes($value)))
    }.GetNewClosure()

Notice the :: instead of .

Upvotes: 3

Related Questions