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

Question about passing array to a .Net method

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-06-2006   #1 (permalink)
=?Utf-8?B?TWlrZSBCcmlkZ2U=?= bridgecanada _ com>
Guest


 

Question about passing array to a .Net method

Hi-

I was trying to instantiate some .Net objects from PowerShell, and I am
confused about why this happens when I pass an array to the constructor:

PS [Scripts] > $bytes=@([System.Text.Encoding]::UTF8.GetBytes("Hello
There"))
PS [Scripts] > new-object System.IO.MemoryStream($bytes)
New-Object : Cannot find an overload for ".ctor" and the argument count: "11".
At line:1 char:11
+ new-object <<<< System.IO.MemoryStream($bytes)

I see that this will work if I do this:

PS [Scripts] > new-object System.IO.MemoryStream(,$bytes)

However, I don't understand why my byte array of length 11 is getting passed
as 11 parameters instead of as an array. Does anyone know why this happens?
Is this the correct way to do this?

Thanks!

-Mike


My System SpecsSystem Spec
Old 10-06-2006   #2 (permalink)
/\\/\\o\\/\\/ [MVP]
Guest


 

Re: Question about passing array to a .Net method

you "force" it into an array of Objects by @()
this is often handy but in this case not needed / wanted.

PS C:\PowerShell> $bytes= [System.Text.Encoding]::UTF8.GetBytes("Hello
There")
PS C:\PowerShell> ,$bytes | gm


TypeName: System.Byte[]

PS C:\PowerShell> ,@() | gm


TypeName: System.Object[]

Greetings /\/\o\/\/

"Mike Bridge bridgecanada _ com>" <mike -> wrote in message
news:43FA0140-6422-484A-907A-5285BE1B6C7E@microsoft.com...
> Hi-
>
> I was trying to instantiate some .Net objects from PowerShell, and I am
> confused about why this happens when I pass an array to the constructor:
>
> PS [Scripts] > $bytes=@([System.Text.Encoding]::UTF8.GetBytes("Hello
> There"))
> PS [Scripts] > new-object System.IO.MemoryStream($bytes)
> New-Object : Cannot find an overload for ".ctor" and the argument count:
> "11".
> At line:1 char:11
> + new-object <<<< System.IO.MemoryStream($bytes)
>
> I see that this will work if I do this:
>
> PS [Scripts] > new-object System.IO.MemoryStream(,$bytes)
>
> However, I don't understand why my byte array of length 11 is getting
> passed
> as 11 parameters instead of as an array. Does anyone know why this
> happens?
> Is this the correct way to do this?
>
> Thanks!
>
> -Mike
>



My System SpecsSystem Spec
Old 10-06-2006   #3 (permalink)
=?Utf-8?B?TWlrZSBCcmlkZ2U=?= bridgecanada _ com>
Guest


 

Re: Question about passing array to a .Net method

Thanks for the response, but my question is actually not "what do I do", but
"why is this happening?". And I did actually try the "don't mess with my
array" operator before I posted (knowing that it saves my arrays with length
of one from being munged) and it didn't work:

PS [Scripts] > new-object System.IO.MemoryStream(@($bytes))
New-Object : Cannot find an overload for ".ctor" and the argument count: "11".
At line:1 char:11
+ new-object <<<< System.IO.MemoryStream(@($bytes))

My frustrated post to the "Problem with parameter passing?" thread is a more
complete expression of my confusion with regard to array-handling. In a
nutshell, I seem to be incapable of arriving at the "Platonic Array Handling
Form" behind all the myriad rules that the parser is applying to my arrays.

Thanks,

-Mike



"/\\/\\o\\/\\/ [MVP]" wrote:

> you "force" it into an array of Objects by @()
> this is often handy but in this case not needed / wanted.
>
> PS C:\PowerShell> $bytes= [System.Text.Encoding]::UTF8.GetBytes("Hello
> There")
> PS C:\PowerShell> ,$bytes | gm
>
>
> TypeName: System.Byte[]
>
> PS C:\PowerShell> ,@() | gm
>
>
> TypeName: System.Object[]
>
> Greetings /\/\o\/\/
>
> "Mike Bridge bridgecanada _ com>" <mike -> wrote in message
> news:43FA0140-6422-484A-907A-5285BE1B6C7E@microsoft.com...
> > Hi-
> >
> > I was trying to instantiate some .Net objects from PowerShell, and I am
> > confused about why this happens when I pass an array to the constructor:
> >
> > PS [Scripts] > $bytes=@([System.Text.Encoding]::UTF8.GetBytes("Hello
> > There"))
> > PS [Scripts] > new-object System.IO.MemoryStream($bytes)
> > New-Object : Cannot find an overload for ".ctor" and the argument count:
> > "11".
> > At line:1 char:11
> > + new-object <<<< System.IO.MemoryStream($bytes)
> >
> > I see that this will work if I do this:
> >
> > PS [Scripts] > new-object System.IO.MemoryStream(,$bytes)
> >
> > However, I don't understand why my byte array of length 11 is getting
> > passed
> > as 11 parameters instead of as an array. Does anyone know why this
> > happens?
> > Is this the correct way to do this?
> >
> > Thanks!
> >
> > -Mike
> >

>
>
>

My System SpecsSystem Spec
Old 10-07-2006   #4 (permalink)
john
Guest


 

Re: Question about passing array to a .Net method

I was just reading an earlier post with the subject "Re: Peculiarity/bug of
foreach-object" and it looks like it is related.

Your observation must be correct about using the unary operator to suppress
the default behavior of unraveling of the array before passing it to the
constructor.

HTH

John

<snip>


The foreach-object cmdlet works like all cmdlets - if the output object is a
collection, it gets unraveled. In a cmdlet you have to specify an option to
suppress this behaviour. In foreach-object, as you observed, the way to
suppress it is to use the unary comma operator

PS (1) > $a = 1,(2,3)
PS (2) > $a.length
2
PS (3) > $a[1]
2
3
PS (4) > $b = $a | foreach {, $_ }
PS (5) > $b.length
2
PS (6) > $b[1]
2
3

When chaining foreach statements, just repeat the pattern...

PS (7) > $b = $a | foreach {, $_ } | foreach { ,$_}
PS (8) > $b.length
2
PS (9) > $b[1]
2
3

Both behaviours are useful. Consider getting a list of loaded module names:

get-process | %{$_.modules} | sort -u modulename

Here the unraveling is exactly what we want. On average, unraveling by
default is most useful but it does present something of a cognitive bump...

-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx

</snip>


"Mike Bridge bridgecanada _ com>" <mike -> wrote in message
news:FE732EFC-19DE-4BC2-8B20-94B87006FCE6@microsoft.com...
> Thanks for the response, but my question is actually not "what do I do",
> but
> "why is this happening?". And I did actually try the "don't mess with my
> array" operator before I posted (knowing that it saves my arrays with
> length
> of one from being munged) and it didn't work:
>
> PS [Scripts] > new-object System.IO.MemoryStream(@($bytes))
> New-Object : Cannot find an overload for ".ctor" and the argument count:
> "11".
> At line:1 char:11
> + new-object <<<< System.IO.MemoryStream(@($bytes))
>
> My frustrated post to the "Problem with parameter passing?" thread is a
> more
> complete expression of my confusion with regard to array-handling. In a
> nutshell, I seem to be incapable of arriving at the "Platonic Array
> Handling
> Form" behind all the myriad rules that the parser is applying to my
> arrays.
>
> Thanks,
>
> -Mike
>
>
>
> "/\\/\\o\\/\\/ [MVP]" wrote:
>
>> you "force" it into an array of Objects by @()
>> this is often handy but in this case not needed / wanted.
>>
>> PS C:\PowerShell> $bytes= [System.Text.Encoding]::UTF8.GetBytes("Hello
>> There")
>> PS C:\PowerShell> ,$bytes | gm
>>
>>
>> TypeName: System.Byte[]
>>
>> PS C:\PowerShell> ,@() | gm
>>
>>
>> TypeName: System.Object[]
>>
>> Greetings /\/\o\/\/
>>
>> "Mike Bridge bridgecanada _ com>" <mike -> wrote in message
>> news:43FA0140-6422-484A-907A-5285BE1B6C7E@microsoft.com...
>> > Hi-
>> >
>> > I was trying to instantiate some .Net objects from PowerShell, and I am
>> > confused about why this happens when I pass an array to the
>> > constructor:
>> >
>> > PS [Scripts] > $bytes=@([System.Text.Encoding]::UTF8.GetBytes("Hello
>> > There"))
>> > PS [Scripts] > new-object System.IO.MemoryStream($bytes)
>> > New-Object : Cannot find an overload for ".ctor" and the argument
>> > count:
>> > "11".
>> > At line:1 char:11
>> > + new-object <<<< System.IO.MemoryStream($bytes)
>> >
>> > I see that this will work if I do this:
>> >
>> > PS [Scripts] > new-object System.IO.MemoryStream(,$bytes)
>> >
>> > However, I don't understand why my byte array of length 11 is getting
>> > passed
>> > as 11 parameters instead of as an array. Does anyone know why this
>> > happens?
>> > Is this the correct way to do this?
>> >
>> > Thanks!
>> >
>> > -Mike
>> >

>>
>>
>>



My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Copy() method in Excel Michael Gao PowerShell 0 07-08-2008 05:12 AM
Extension Method Question for ServicedComponent coconet .NET General 1 04-06-2008 03:23 PM
Newbie question - Copying files listed in an array greatbarrier86 PowerShell 5 02-20-2008 04:03 PM
Passing a string array as a parameter from one script to another Brillig PowerShell 5 09-27-2007 04:16 AM
Passing array to .Net command: discrete parameters =?Utf-8?B?TWlrZSBCcmlkZ2U=?= bridgecanada _ com> PowerShell 1 10-06-2006 04:12 PM


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