Moerwald
Moerwald

Reputation: 11304

Create new System.Collections.Generic.Dictionary object fails in PowerShell

PowerShell version: 5.x, 6

I'm trying to create a new object of System.Collections.Generic.Dictionary, but it fails.

I tried the following "versions":

> $dictionary = new-object System.Collections.Generic.Dictionary[[string],[int]]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ry = new-object System.Collections.Generic.Dictionary[[string],[int]]
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand

> $dictionary = new-object System.Collections.Generic.Dictionary[string,int]
New-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComObject'. Specified method is not supported.
At line:1 char:25
+ ... ionary = new-object System.Collections.Generic.Dictionary[string,int]
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewObjectCommand

I know that I can use a hashtable under PowerShell, but I want to know how to create a dictionary via the above declaration.

What am I missing?

Thx

Upvotes: 3

Views: 12142

Answers (4)

dzampino
dzampino

Reputation: 133

Since I didn't see this variant in the other answers, this works in at least v5+

$dictionary = New-Object 'System.Collections.Generic.Dictionary[string,int]'

Upvotes: 0

TechSpud
TechSpud

Reputation: 3528

As well as the accepted answer, a dictionary can also be initialised using the syntax in the code block below, which:

  • removes the need to escape any characters
  • is much cleaner (in my opinion)
  • gives you intellisense (tested in PowerShell & PowerShell ISE)
$dictionary = [System.Collections.Generic.Dictionary[string,int]]::new()

... where string and int are .NET types.

Upvotes: 6

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

The problem is how is interpreting your argument.

When you include a comma in your string, it's now trying to bind

'System.Collections.Generic.Dictionary[[string]', '[int]]'

to the -TypeName parameter which is of type <string[]> or in the error message, <System.Object[]>. This can be solved by properly quoting your argument so it matches the expected parameter binding of <string>:

New-Object -TypeName 'System.Collections.Generic.Dictionary[[string], [int]]'

Upvotes: 3

JosefZ
JosefZ

Reputation: 30153

Used type name System.Collections.Generic.Dictionary[[string],[int]] contains a comma. By Creating and initializing an array:

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma

Hence, you need to escape the comma (read the about_Escape_Characters and about_Quoting_Rules help topics). There are more options:

In Windows PowerShell, the escape character is the backtick (`), also called the grave accent (ASCII 96).

$dictionary = new-object System.Collections.Generic.Dictionary[[string]`,[int]]

Quotation marks are used to specify a literal string. You can enclose a string in single quotation marks (') or double quotation marks (").

$dictionary = new-object "System.Collections.Generic.Dictionary[[string],[int]]"

or

$dictionary = new-object 'System.Collections.Generic.Dictionary[[string],[int]]'

Upvotes: 8

Related Questions