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

  3. #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
I have a Windows Server 2008 with the PowerShell 2 CTP installed. I was attempting to install a role/feature that requires PowerShell 1.0 so I...
PowerShell
4 the powershell team integrate powershell with onenote
been getting up close and personal with ms onenote 2007 this weekend for a work project and though it would be cool to have powershell integrated...
PowerShell
when run powershell script as windows service ,powershell fail
On windows 2008 ,run powershell script by windows serivice application ,but this script always failed due to the following .Net runtime error ...
PowerShell
Powershell Plus - Free for non commercial Use and Powershell Analyzer1.0 released
Head over to http://blog.powershell.com to read about this. Basically PSPLUS beta is available with a 45 day trial, and after that free for non...
PowerShell
Automatic PowerShell Error Parsing in PowerShell Analyzer and PowerShellPlus
PowerShell Analyzer and PowerShell Plus now have automatic PowerShell syntax parsing of PowerShell Script. see a screenshot and read more about...
PowerShell
Powershell scripts not working with Vista powershell
Hi, I am still in the process of learning Powershell and I am in the middle of my transition from windows XP to Vista.. I was trying to run one...
PowerShell
PowerShell Leaders Join Forces and offer a pre-release version of PowerShell for 50% off the retail value
Shell Tools officially announced that Dr. Tobias Weltner (MVP); author of the award winning "System Scripter" has joined the company. Weltner...
PowerShell