On Dec 11, 7:40 am, Colin Bowern <colinbow...@xxxxxx>
wrote:
> I'm trying to create a Predicate using this method but the result isn't
> correct:
>
> $domains = New-Object "System.Collections.Generic.List``1[System.String]";
>
> $domains.Add("foo.com");
>
> $uri = New-Object System.Uri("http://bar.com");
>
> $delegate = get-delegate "System.Predicate``1[System.String]" { Write-Host
> $uri.Host " : " $args " = " ([String]::Compare($uri.Host, $args[0], $true)
> -eq 0); return ([String]::Compare($uri.Host, $args[0], $true) -eq 0); };
>
> $domains.Exists($delegate);
>
> The call to Exists returns true even though bar.com isn't in the collection.
>
> Thoughts on how where I'm going wrong?
>
> Thanks,
> Colin I noticed this problem before too Colin; it appears to be a bug in the
get-delegate script (or more specifically, the emitted msil).
PS> $d = .\get-delegate.ps1
"System.Predicate``1[System.String]" { $false }
PS> $d.Invoke("foo")
True
Ultimately, the emitted MSIL is trying to do something similar to
this:
[System.Management.Automation.LanguagePrimitives]::ConvertTo(
( { $false } ).InvokeReturnAsIs("foo"), [bool] )
....although Lee (I think he wrote this?) also uses some trickery with
the new performance friendly RuntimeTypeHandles. I never investigated
it further, but I've a feeling it may be something to do with the
LanguagePrimitives overload resolution and/or getting overly
optimistic about the boolean coercion. I'm curious now about what's
really happening and will take a look later on to see if I can fix it;
I'll post back here with results.
- Oisin / x0n