![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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 | 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |