Type Accelerator?

Welcome Forums General PowerShell Q&A Type Accelerator?

Viewing 7 reply threads
  • Author
    Posts
    • #217200
      Participant
      Topics: 7
      Replies: 556
      Points: 2,116
      Helping Hand
      Rank: Community Hero

      I’ve asked this question to colleagues and of course google. I’m assuming it has something to do with it being a type accelerator, but from what I’ve read my understanding is that type accelerators help you to not have to type the full .Net type. So why can’t I do it the long way? I’m looking for actual technical reasons, not “because Bruce Payette said so” or the like. 🙂

      This

      results in error

      But this

      works just fine, get-member shows

      And then lastly

      Is just a hash table

      I know it’s probably something dumb and I’m an idiot for asking. But it’s always bugged me.

    • #217401
      Participant
      Topics: 1
      Replies: 13
      Points: 20
      Rank: Member

      Interesting topic !

      I’ve tried your cast inside a Trace-Command block… and getting no more useful info.

      I’ve assumed also it might be a parser thing (maybe Bruce 🙂  checked for this specific cast only for the type accelerator name, and not for the full base type, or the System namespace but not System.Management.Automation, or whatever …)

      But it is not the parser. It seems something inside .NET, as doing it in two steps also fails the same way :

      </p><p>$var1 = @{<br />Name = 'test'<br />Admin = $true<br />}   # OK<br />$Var2 = [System.Management.Automation.PSCustomObject]$Var1                 # Same error</p><p>

      BTW,

      </p><p>$var -IS  [System.management.automation.pscustomobject]      # returns $true</p><p>

      Next step would be checking the PowerShell source code…

    • #217449
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,686
      Helping Hand
      Rank: Community Hero

      Great question. I think somehow it’s the equivalent of:

      But even this still results in a hashtable:

      Get type accelerator: https://powershellexplained.com/2016-10-28-powershell-everything-you-wanted-to-know-about-pscustomobject/

      • This reply was modified 5 months, 1 week ago by js.
      • This reply was modified 5 months, 1 week ago by js.
      • This reply was modified 5 months, 1 week ago by js.
      • This reply was modified 5 months, 1 week ago by js.
      • This reply was modified 5 months, 1 week ago by js.
    • #217473
      Participant
      Topics: 7
      Replies: 556
      Points: 2,116
      Helping Hand
      Rank: Community Hero

      Dang it, Bruce! Thanks for chiming in guys, I’ve already learned a couple of things from you both.

    • #217479
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,686
      Helping Hand
      Rank: Community Hero

      I’ve been digging around. It might just be a bug or non-optimal behavior that would require a breaking change? For example: https://github.com/PowerShell/PowerShell/issues/2295

      • This reply was modified 5 months, 1 week ago by js.
      • This reply was modified 5 months, 1 week ago by js.
    • #217488
      Participant
      Topics: 7
      Replies: 556
      Points: 2,116
      Helping Hand
      Rank: Community Hero

      That is exactly the issue, it tabbed completed and didn’t work.  I am curious (and annoyed) with my favorite search engine that I wasn’t able to locate this. Thank you so much for spending the time to find this.

    • #217497
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,686
      Helping Hand
      Rank: Community Hero

      Looks like the magic is in the source code: https://stackoverflow.com/a/61141536/6654942

    • #217500
      Participant
      Topics: 7
      Replies: 556
      Points: 2,116
      Helping Hand
      Rank: Community Hero

      Absolutely. I love the comments.

      // We can’t use the normal PSConvertBinder because it is strongly typed, and we
      // play some funny games with the PSCustomObject type (externally, it’s just an
      // alias for PSObject, internally we do other stuff with it.)

Viewing 7 reply threads
  • The topic ‘Type Accelerator?’ is closed to new replies.