PatrikJ PatrikJ - 3 months ago 26
PowerShell Question

Powershell Add-Type - Add member of type dictionary/hashtable

I am trying to add a custom type in powershell like this:

Add-Type @'
public class MyType {
public string name;
public string type;
public hashtable data;
public string category;
}
'@


However, the type hashtable, dictionary does not exist. I have looked around for examples regarding this but could not find any.

Might add, that I do not want to use Add-Member.

Any ideas?

/Patrik

Answer

2 things:

  1. Capitalize "Hashtable", C# is case-sensitive
  2. Add a using statement, so the compiler can resolve the full type name

Add-Type @'
using System.Collections;

public class MyType {
    public string name;
    public string type;
    public Hashtable data;
    public string category;
}
'@
Comments