Note that there are some explanatory texts on larger screens.

plurals
  1. POLosing type information when passing an object through a System.Collections.Queue
    text
    copied!<p>As part of my Powershell script I need to, essentially, implement breadth-first search. So, I need queue and figured System.Collections.Queue is as good as any other queue. However, when I get an object out of the queue, Powershell loses track of any properties I've added to the object.</p> <p>What's going on here? How do I access my property?</p> <p>This sample demonstrates the problem:</p> <pre><code>$object = new-object object add-member -membertype noteproperty -name AnInteger -value 2 -inputobject $object $queue = new-object system.collections.queue $queue.enqueue($object) $dequeued = $queue.dequeue() # 1. True - both variables refer to the same object. $object -eq $dequeued # 2. True - using the original variable, can access the property. $object.AnInteger -ne $null # 3. False. Err, What? $dequeued.AnInteger -ne $null </code></pre> <h1>Solution and More Questions</h1> <p>I found my mistake: <code>add-member</code> modifies an instance of <code>PSObject</code> not Object. It looks like it created one for me to wrap <code>$object</code> before adding the property. So, I needed to either create a <code>PSObject</code> or cast the result of dequeue to <code>PSObject</code>. </p> <p>I still don't understand everything that's going on here. Did <code>add-member</code> modify <code>$object</code> to refer to the <code>PSObject</code> it created? If not, how did the Powershell run-time know that <code>$object</code> was in fact a <code>PSObject</code>? <code>$object.gettype().name</code> is <code>Object</code> not <code>PSCustomObject</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload