In PowerShell you can create a hashtable and add this hashtable to your function with an @
, which is splatting in PowerShell.
$dict = @{param1 = 'test'; param2 = 12}
Get-Info @dict
Can one pass a dictionary as a collection of parameters to a constructor or method?
If you want to support multiple type values in a dictionary, similar to your PowerShell Hash Table, you could create a
Dictionary<string, object>
here:Which could then be passed to a constructor:
The
object
type is just theSystem.Object
class defined in .NET, which all types inherit from. You can check this in PowerShell with$dict.GetType().BaseType.FullName
, which will give youSystem.Object
.However, using
object
as shown above is dangerous as it provides no type safety and requires you to cast to and fromobject
. This is also known as Boxing and Unboxing. It would be better to use strong types here if you can, or rethink your design.With the above in mind, we could use simple class here instead with strongly typed attributes:
Then you could just initialize it like this:
And be passed to your constructor: