Forcing the issue when PowerShell's ctor selection magic goes wrong

I'll just include the thread below, but I was trying to force PowerShell to call the correct 4-param version of the FileStream ctor but my attempt to force the types of params 2, 3, and 4 wasn't working as I thought :)

Thankfully, Jim Truher fixed me up and pointed out that I wasn't really calling with the enum params in my second attempt like I thought I was :)

************************************************

In this context, [io.filemode]::append is actually a string (try write-output [io.filemode]::append to see), if you want these to be actually objects, you need to change the precedence and coerce them before calling the new-object.  You just need this:

$fs = new-object system.io.filestream 'c:\blah.txt', ([io.filemode]::append), ([io.fileaccess]::write), ([io.fileshare]::none)

The parens force them to be evaluated, which returns objects which are then used by new-object

jim

From: James Manning
Sent: Friday, January 05, 2007 11:34 AM
Subject: PowerShell didn't figure out the right ctor to call?

This is likely PEBKAC, but I’m not seeing it.

I’m trying to call the (string, filemode, fileaccess, fileshare) ctor for FileStream.  In doing so, it looks like PS is coming up with the wrong ctor to attempt to call.

C:\Users\jmanning# $fs = new-object 'system.io.filestream' 'c:\blah.txt', 'append'

C:\Users\jmanning# $fs.close()

C:\Users\jmanning# $fs = new-object 'system.io.filestream' 'c:\blah.txt', 'append', 'write'

C:\Users\jmanning# $fs.close()

C:\Users\jmanning# $fs = new-object 'system.io.filestream' 'c:\blah.txt', 'append', 'write', 'none'

New-Object : Cannot convert argument "0", with value: "c:\blah.txt", for ".ctor" to type "System.IntPtr": "Cannot convert value "c:\blah.txt" to type "System.IntPtr". Error: "Invalid cast from 'System.String' to 'System.IntPtr'.""

At line:1 char:17

+ $fs = new-object  <<<< 'system.io.filestream' 'c:\blah.txt', 'append', 'write', 'none'

C:\Users\jmanning# $fs = new-object 'system.io.filestream' [string]'c:\blah.txt', [io.filemode]::append, [io.fileaccess]::write, [io.fileshare]::none

New-Object : Cannot convert argument "0", with value: "[string]c:\blah.txt", for ".ctor" to type "System.IntPtr": "Cannot convert value "[string]c:\blah.txt" to type "System.IntPtr". Error: "Invalid cast from 'System.String' to 'System.IntPtr'.""

At line:1 char:17

+ $fs = new-object  <<<< 'system.io.filestream' [string]'c:\blah.txt', [io.filemode]::append, [io.fileaccess]::write, [io.fileshare]::none

C:\Users\jmanning#

I’m sure I’ll either figure this out right after I send this or the answer will be blindingly obvious to others, but I’d appreciate the pointer into what I’m doing wrong

James