![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | How to put PSObject into System.Collections.ObjectModel.Collection I want to add some properties to .NET objects and store the objects in a System.Collections.ObjectModel.Collection. And when I later access the Collection using .Item($index) I want those objects back including the properties I added at the beginning. In RC1 I was using some code similar to this, but now it doesn't seem to work anymore. # get the AssemblyQualifiedName of psobject $pso = (New-Object psobject).GetType().AssemblyQualifiedName # create a collection that can hold psobjects $list = New-Object "System.Collections.ObjectModel.Collection``1[[$pso]]" # create a psobject [psobject]$temp = New-Object psobject # add some properties to it Add-Member -input $temp -membertype noteproperty -name prop -value 5 # store it in the collection $liste.add($temp) I tried to use a Collection of Object, but then all the added properties seem to disappear. I'm sure that something has changed here since RC1. Also, PSObject will always be automatically converted to PSCustomObject. Any ideas? -- greetings dreeschkind |
My System Specs![]() |
| | #2 (permalink) |
| Guest | RE: How to put PSObject into System.Collections.ObjectModel.Collection The last line should read: $list.add($temp) -- greetings dreeschkind "dreeschkind" wrote: > I want to add some properties to .NET objects and store the objects in a > System.Collections.ObjectModel.Collection. And when I later access the > Collection using .Item($index) I want those objects back including the > properties I added at the beginning. In RC1 I was using some code similar to > this, but now it doesn't seem to work anymore. > > # get the AssemblyQualifiedName of psobject > $pso = (New-Object psobject).GetType().AssemblyQualifiedName > > # create a collection that can hold psobjects > $list = New-Object "System.Collections.ObjectModel.Collection``1[[$pso]]" > > # create a psobject > [psobject]$temp = New-Object psobject > > # add some properties to it > Add-Member -input $temp -membertype noteproperty -name prop -value 5 > > # store it in the collection > $liste.add($temp) > > I tried to use a Collection of Object, but then all the added properties > seem to disappear. I'm sure that something has changed here since RC1. Also, > PSObject will always be automatically converted to PSCustomObject. Any ideas? > > -- > greetings > dreeschkind |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: How to put PSObject into System.Collections.ObjectModel.Collection does this work for you $list = new-Object system.Collections.ArrayList $temp = 1 |Select-Object prop, prop2 | % { $_.prop = 5 ; $_ } $list.add($temp) $list.Add($(1 |Select-Object prop, prop2 | % { $_.prop = 10 ; $_ }) ) $list i'm using select object as an easy way to make a Pscustomobject, and using an arraylist rather than a strongly typed collection, just because its easier |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: How to put PSObject into System.Collections.ObjectModel.Collec "klumsy@xtra.co.nz" wrote: > does this work for you It works with PSCustomObjects that I create by myself, but it does not work with 'normal' .Net Objects that have added properties. Once you pull them out of the ArrayList, all the custom properies are gone. ![]() Any other ideas? -- greetings dreeschkind |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: How to put PSObject into System.Collections.ObjectModel.Collection something interesting is happening here.. in the example i just submitted all works with a pscustomobject, however if you take any old object, and spotweld on some properties with add-member , it seems that when you add this to a collection,arraylist.. the added properties get stripped off, as it its just hte baseobject that gets added.. loosing the custom properties we added $list = New-Object "System.Collections.ObjectModel.Collection``1[[$pso]]" $list = new-Object system.Collections.ArrayList $temp = 1 |Select-Object prop, prop2 | % { $_.prop = 5 ; $_ } $list.add($temp) $list.Add($(1 |Select-Object prop, prop2 | % { $_.prop = 10 ; $_ }) ) #ok so it all works so far $temp = (gci)[0] $temp = add-member -input $temp -passthru -member noteproperty -name prop -value 5 $list.Add($temp) #ok this one (above) when we retrieve it from the list, the property is gone.. its like the custom info got stripped off when adding to the collection. $temp = (gci)[1] $temp = add-member -input $temp -passthru -member noteproperty -name prop -value 10 $list.Add(,$temp) $list #but when i add the , to wrap it in a one level array. it works fine, as it must just strip off the one itemed array that wrapped the ps object. interesting, and maybe buggy behaviour? |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: How to put PSObject into System.Collections.ObjectModel.Collec Thanks again klumsy, the trick using an ArrayList and an extra "," when adding the object to the ArrayList works perfectly: $list = new-Object system.Collections.ArrayList $temp = (gci)[0] $temp = add-member -input $temp -passthru -member noteproperty -name prop -value 5 $list.Add(,$temp) I had some more problems accessing the added properties of the objects in the collection, but klumsy could also help me with this issue on IRC, so now everything works as it should: # doesn't work: $list.item(0).prop $list[0].prop ($list[0]).prop $g = $list[0] $g = $list[0]; $g.prop # works: $($list[0]).prop 5 $($list.item(0)).prop 5 $g = $list.item(0); $($g).prop 5 $h = $($list.item(0)); $h.prop 5 -- greetings dreeschkind "klumsy@xtra.co.nz" wrote: > something interesting is happening here.. in the example i just > submitted all works with a pscustomobject, however if you take any old > object, and spotweld on some properties with add-member , it seems that > when you add this to a collection,arraylist.. the added properties get > stripped off, as it its just hte baseobject that gets added.. loosing > the custom properties we added > > $list = New-Object > "System.Collections.ObjectModel.Collection``1[[$pso]]" > > $list = new-Object system.Collections.ArrayList > $temp = 1 |Select-Object prop, prop2 | % { $_.prop = 5 ; $_ } > $list.add($temp) > $list.Add($(1 |Select-Object prop, prop2 | % { $_.prop = 10 ; $_ }) ) > > #ok so it all works so far > $temp = (gci)[0] > $temp = add-member -input $temp -passthru -member noteproperty -name > prop -value 5 > $list.Add($temp) > > #ok this one (above) when we retrieve it from the list, the property is > gone.. its like the custom info got stripped off when adding to the > collection. > > $temp = (gci)[1] > $temp = add-member -input $temp -passthru -member noteproperty -name > prop -value 10 > $list.Add(,$temp) > $list > > #but when i add the , to wrap it in a one level array. it works fine, > as it must just strip off the one itemed array that wrapped the ps > object. > > interesting, and maybe buggy behaviour? > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Confused about PSObject... | tmy | PowerShell | 1 | 08-11-2008 04:51 PM |
| System.Collections.Generic.List<int> myList = new System.Collections.Generic.List<int>(100); | DR | .NET General | 1 | 04-09-2008 02:29 PM |
| psobject non-serializable | William Stacey [C# MVP] | PowerShell | 0 | 04-09-2007 03:31 PM |
| How to create a collection of psObject? | Sylvain Mottet | PowerShell | 5 | 08-04-2006 10:42 PM |
| How create a collection of psObject | Sylvain Mottet | PowerShell | 0 | 08-04-2006 03:32 AM |