I have a c# method I am loading from a dll with optional string arguments that default to null. For example
public void foo(string path, string new_name = null, bool save_now = true) { if(name == null) new_name = Path.GetFileNameWithoutExtension(path); ... if(save_now) Save(); } I want to call this from within a powershell script and not supply a value for new_name but one for save_now. As per this seemingly very similar question I have tried
$default = [type]::Missing $obj.foo($path, $default, $false) but this results in new_name being set as "System.Reflection.Missing" within the function.
Additionally I tried
$obj.foo($path, $null, $false) but this results in new_name being set to the empty string, still not null. I could set the default to the empty string, but I was wondering if there was any good way to actually have the default value be used.
GetMethodreturns aMethodInfo, which has aGetPropertymethod. It returns aParameterInfowhich hasDefaultValue, HasDefaultValue, and IsOptionalproperties. So you could develop a PS function, say "InvokeWithNamedParameters" that emulates what VB supports.