【问题标题】:Powershell Add-Type - Add member of type dictionary/hashtablePowershell Add-Type - 添加类型字典/哈希表的成员
【发布时间】:2017-01-05 07:22:12
【问题描述】:

我正在尝试在 powershell 中添加一个自定义类型,如下所示:

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

但是,hashtable、dictionary 类型不存在。我四处寻找有关此的示例,但找不到任何示例。

可能会补充一点,我不想使用 Add-Member。

有什么想法吗?

/帕特里克

【问题讨论】:

    标签: class powershell dictionary hashtable add-type


    【解决方案1】:

    两件事:

    1. 大写“Hashtable”,C# 区分大小写
    2. 添加using 语句,以便编译器解析完整的类型名称

    Add-Type @'
    using System.Collections;
    
    public class MyType {
        public string name;
        public string type;
        public Hashtable data;
        public string category;
    }
    '@
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2023-03-27
      • 2016-03-31
      • 2018-07-18
      • 1970-01-01
      • 2021-10-21
      • 2016-10-14
      • 1970-01-01
      • 2012-02-26
      相关资源
      最近更新 更多