如何在 PowerShell v5 模块中导出类 [英] How to export a class in a PowerShell v5 module

查看:41
本文介绍了如何在 PowerShell v5 模块中导出类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块设置,就像其他一些脚本的库一样.我不知道如何将类声明放入调用 导入模块.我试图用 -class 参数安排 Export-Module,就像 -function,但没有 -class 可用.我是否只需要在每个脚本中声明类?

I've got a module setup to be like a library for a few other scripts. I can't figure out how to get a class declaration into the script scope calling Import-Module. I tried to arrange Export-Module with a -class argument, like the -function, but there isn't a -class available. Do I just have to declare the class in every script?

设置:

  • holidays.psm1 位于 ~\documents\windows\powershell\modules\holidays\
  • 活动脚本调用导入模块假期
  • holiday.psm1 中有另一个函数可以正确返回类对象,但我不知道如何在导入后从活动脚本创建类的新成员

这是类的样子:

Class data_block
{
    $array
    $rows
    $cols
    data_block($a, $r, $c)
    {
        $this.array = $a
        $this.rows = $r
        $this.cols = $c
    }
}

推荐答案

根据此处这里,您可以通过在 PowerShell 5 中执行以下操作来使用模块中定义的类:

According to here and here, you can use classes defined in your module by doing the following in PowerShell 5:

using module holidays

这篇关于如何在 PowerShell v5 模块中导出类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆