列一个清单通过 powershell 中的 Add-Type -TypeDefinition 创建的类型

发布于 2024-12-03 10:05:00 字数 1074 浏览 0 评论 0原文

我有一个在 powershell 中的内联 C# 中定义的类:

Add-Type -TypeDefinition @"
    public class SomeClass {
        string name;
        public string Name { get { return name; } set { name = value; } }
        int a, b;
        public int A { get { return a; } set { a = value; } }
        public int B { get { return b; } set { b = value; } }
    }
"@

我可以实例化它: $someClass= 新对象 SomeClass -Property @{ '姓名' = “贾斯汀·迪林”; “A”=1; “B”=5; };

但我无法实例化它的列表:

$listOfClasses = New-Object System.Collections.Generic.List[SomeClass];

这样做可以让我得到以下结果:

New-Object : Cannot find type [[System.Collections.Generic[SomeClass]]]: make sure the assembly containing this type is loaded.
At line:12 char:28
+ $listOfClasses = New-Object <<<<  [System.Collections.Generic[SomeClass]]
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

I have a class that I define in inline C# in powershell:

Add-Type -TypeDefinition @"
    public class SomeClass {
        string name;
        public string Name { get { return name; } set { name = value; } }
        int a, b;
        public int A { get { return a; } set { a = value; } }
        public int B { get { return b; } set { b = value; } }
    }
"@

I can instantiate it:
$someClass= New-Object SomeClass -Property @{
'Name' = "Justin Dearing";
"A" = 1;
"B" = 5;
};

But I cannot instantiate a list of it:

$listOfClasses = New-Object System.Collections.Generic.List[SomeClass];

Doing so gets me the following:

New-Object : Cannot find type [[System.Collections.Generic[SomeClass]]]: make sure the assembly containing this type is loaded.
At line:12 char:28
+ $listOfClasses = New-Object <<<<  [System.Collections.Generic[SomeClass]]
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿光 2024-12-10 10:05:00

PowerShell 似乎不直接支持。这是一个解决方案,提供一个 New-GenericObject 脚本;它会进行一些反射以创建正确的类型。

It seems that is not directly supported in PowerShell. Here is a solution, which provides a New-GenericObject script; that does some reflection in order to create the correct type.

也只是曾经 2024-12-10 10:05:00

一种解决方案是简单地创建一个 List。工厂在 SomeClass 定义中,如下所示:

Add-Type -TypeDefinition @"
    using System.Collections.Generic;

    public class SomeClass {
        string name;
        public string Name { get { return name; } set { name = value; } }
        int a, b;
        public int A { get { return a; } set { a = value; } }
        public int B { get { return b; } set { b = value; } }

        public static List<SomeClass> CreateList () { return new List<SomeClass>(); }
    }
"@

然后您可以像这样实例化一个列表 $listOfClasses = [SomeClass]::CreateList();

One solution is to simple make a List<SomeClass> factory in the SomeClass definition like so:

Add-Type -TypeDefinition @"
    using System.Collections.Generic;

    public class SomeClass {
        string name;
        public string Name { get { return name; } set { name = value; } }
        int a, b;
        public int A { get { return a; } set { a = value; } }
        public int B { get { return b; } set { b = value; } }

        public static List<SomeClass> CreateList () { return new List<SomeClass>(); }
    }
"@

Then you can instantiate a list like so $listOfClasses = [SomeClass]::CreateList();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文