Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to put PSObject into System.Collections.ObjectModel.Collection

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-09-2006   #1 (permalink)
dreeschkind
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 SpecsSystem Spec
Old 11-09-2006   #2 (permalink)
dreeschkind
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 SpecsSystem Spec
Old 11-09-2006   #3 (permalink)
klumsy@xtra.co.nz
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 SpecsSystem Spec
Old 11-09-2006   #4 (permalink)
dreeschkind
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 SpecsSystem Spec
Old 11-09-2006   #5 (permalink)
klumsy@xtra.co.nz
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 SpecsSystem Spec
Old 11-09-2006   #6 (permalink)
dreeschkind
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51