clarification about parameter binding

Welcome Forums General PowerShell Q&A clarification about parameter binding

Viewing 6 reply threads
  • Author
    Posts
    • #262958
      Participant
      Topics: 21
      Replies: 22
      Points: 35
      Rank: Member

      both $source and $dest are System.IO.FileInfo objects. I can see through trace-command that it’s taking the value of “C:\foo.log” and converting it to string (the input type for -path). the question is how does it get the value of c:\foo.log. How does it know which of $source’s parameters to use.

       

      Trace-Command ParameterBinding -Expression {Copy-Item -Path $source -Destination $dest -force} -PSHost

      DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Copy-Item]
      DEBUG: ParameterBinding Information: 0 : BIND arg [C:\foo.log] to parameter [Path]
      DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String[]]
      DEBUG: ParameterBinding Information: 0 : Trying to convert argument value from System.IO.FileInfo to
      System.String[]

       

       

    • #263004
      Participant
      Topics: 16
      Replies: 1792
      Points: 3,290
      Helping Hand
      Rank: Community Hero

      This is a educated guess, but most likely it attempts a conversion with ToString().

      As to what ToString() is doing, it appears to get the Fully Qualified Name, which in this case is the FullName. More information about ToString can be found here:
      StackOverflow Thread: https://tinyurl.com/y4v4veum

    • #263049
      Participant
      Topics: 21
      Replies: 22
      Points: 35
      Rank: Member

      As to what ToString() is doing, it appears to get the Fully Qualified Name, which in this case is the FullName. More information about ToString can be found here

      https://docs.microsoft.com/en-us/dotnet/api/system.object.tostring?view=netcore-3.1 talks more about it.

      I never did real dev so don’t know how methods are implemented, overloading, overriding etc. But I got the gist of the idea. It wasn’t binding to any specific parameter but converting the object to a string.

      thanks for the help.

    • #263301
      Senior Moderator
      Topics: 9
      Replies: 1365
      Points: 5,024
      Helping Hand
      Rank: Community MVP

      yes exactly, its a string conversion and AFAIK, done with this internally [System.Management.Automation.LanguagePrimitives]::ConvertTo($Path,’string’)

    • #263579
      js
      Participant
      Topics: 32
      Replies: 847
      Points: 2,723
      Helping Hand
      Rank: Community Hero

      The words are cutoff in my browser.

      I think you mean which of the $source properties. “-path” of of type string, so powershell casts or coerces $source to type string.

    • #263585
      js
      Participant
      Topics: 32
      Replies: 847
      Points: 2,723
      Helping Hand
      Rank: Community Hero

      I can’t edit the last post.

    • #263786
      Senior Moderator
      Topics: 9
      Replies: 1365
      Points: 5,024
      Helping Hand
      Rank: Community MVP

      I can’t edit the last post.



      @js2010
      Fixed. There was a long url text in Rob’s post making the posts non editable. I just removed it.

      • This reply was modified 3 days, 11 hours ago by kvprasoon.
Viewing 6 reply threads
  • You must be logged in to reply to this topic.