Windows Vista Forums

Predicates in PowerShell

  1. #1


    Colin Bowern Guest

    Predicates in PowerShell

    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

      My System SpecsSystem Spec

  2.   


  3. #2


    Oisin Grehan Guest

    Re: Predicates in PowerShell

    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




      My System SpecsSystem Spec

  4. #3


    Colin Bowern Guest

    Re: Predicates in PowerShell

    Thanks Oisin. I bet you are right as I look at the get-delegate code. I
    posted to the PSH blog and hopefully they will pick it up there too. At this
    point I'll probably revert back to writing console apps to get this task
    underway in parallel to this.

    "Oisin Grehan" wrote:

    > 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
    >
    >
    >
    >

      My System SpecsSystem Spec


Predicates in PowerShell
Similar Threads
Thread Forum
Installing PowerShell dependent features on W2K8 with PowerShell CTP PowerShell
when run powershell script as windows service ,powershell fail PowerShell
Powershell Plus - Free for non commercial Use and Powershell Analyzer1.0 released PowerShell
Automatic PowerShell Error Parsing in PowerShell Analyzer and PowerShellPlus PowerShell
PowerShell Leaders Join Forces and offer a pre-release version of PowerShell for 50% off the retail value PowerShell