PowerShell支持面向对象编程(OOP)吗?

10

PowerShell中的Class、Interface、Mixin等概念是什么?它支持面向对象编程吗?如果支持,我应该在哪里阅读相关信息?

4个回答

12

您可以使用 Add-Type 命令在 PowerShell v2.0 中定义新类型:

详细说明

Add-Type 命令允许您在 Windows PowerShell 会话中定义 .NET 类。您可以随后实例化对象(通过使用 New-Object 命令)并使用这些对象,就像使用任何 .NET 对象一样。如果您将 Add-Type 命令添加到 Windows PowerShell 档案文件中,则该类将在所有的 Windows PowerShell 会话中可用。

您可以通过指定现有程序集或源代码文件来指定类型,或者您可以在线或保存在变量中指定源代码。您甚至可以只指定一个方法,Add-Type 就会定义并生成该类。您可以使用此功能在 Windows PowerShell 中调用非托管函数的平台调用 (P/Invoke)。如果您指定源代码,则 Add-Type 将编译指定的源代码,并生成包含新 .NET 类的内存中程序集。

您可以使用 Add-Type 的参数来指定备用语言和编译器 (CSharp 是默认值)、编译器选项、程序集依赖项、类名称空间以及类型和生成的程序集的名称。

要了解更多信息,请使用 help Add-Type 命令。

还请参阅:


8

PowerShell更像是一种面向对象的消费语言。它可以利用大部分.NET Framework,但本身不支持创建接口、类和混合项。 PowerShell的类型系统基于.NET,而.NET不支持混合项。 PowerShell通过Add-Member cmdlet支持对现有对象动态添加属性和方法。

Add-Type很有用,但如果您必须转到C#或VB来定义一个类或实现特定接口的类,那么我不会考虑将其作为优先级支持创建类/接口。

如果您正在寻找一些免费的学习材料,请查看Effective Windows PowerShell


好的回答,非常好的书。谢谢你两个! - Dan K

3

PowerShell 5似乎支持一些主流的面向对象编程。

所有的功劳都归功于这位作者:https://xainey.github.io/2016/powershell-classes-and-concepts/

以下是一个类的示例:

    class myColor
    {
        [String] $Color
        [String] $Hex

        myColor([String] $Color, [String] $Hex)
        {
            $this.Color = $Color
            $this.Hex = $Hex
        }

        [String] ToString()
        {
            return $this.Color + ":" + $this.Hex
        }
    }

抽象类的例子:

class Foo
{
    Foo ()
    {
        $type = $this.GetType()

        if ($type -eq [Foo])
        {
            throw("Class $type must be inherited")
        }
    }

    [string] SayHello()
    {
        throw("Must Override Method")
    }
}

class Bar : Foo
{
    Bar ()
    {

    }

    [string] SayHello()
    {
        return "Hello"
    }
}

1
欢迎提供解决方案的链接,但请确保您的答案即使没有链接也是有用的:在链接周围添加上下文,以便其他用户知道它是什么以及为什么存在,然后引用您链接的页面中最相关的部分,以防目标页面不可用。仅仅是一个链接的答案可能会被删除。 - Xantium

0
PowerShell管道处理对象,而不仅仅是像Unix管道那样的文本流。所有变量也都是对象的实例。顺便说一下,这些都是.NET对象。
这是一个“ls”命令通过管道传递给get-member cmdlet的输出的一部分。
    PS C:\Documents and Settings\Administrator.DEV-3DPST1-SWK> ls | get-member


   TypeName: System.IO.DirectoryInfo

Name                      MemberType     Definition
----                      ----------     ----------
Create                    Method         System.Void Create(DirectorySecurity directorySecurity), System.Void Create()
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
CreateSubdirectory        Method         System.IO.DirectoryInfo CreateSubdirectory(String path), System.IO.Director...
Delete                    Method         System.Void Delete(), System.Void Delete(Boolean recursive)
Equals                    Method         System.Boolean Equals(Object obj)
GetAccessControl          Method         System.Security.AccessControl.DirectorySecurity GetAccessControl(), System....
GetDirectories            Method         System.IO.DirectoryInfo[] GetDirectories(String searchPattern), System.IO.D...
GetFiles                  Method         System.IO.FileInfo[] GetFiles(String searchPattern), System.IO.FileInfo[] G...
GetFileSystemInfos        Method         System.IO.FileSystemInfo[] GetFileSystemInfos(String searchPattern), System...
GetHashCode               Method         System.Int32 GetHashCode()
GetLifetimeService        Method         System.Object GetLifetimeService()
GetObjectData             Method         System.Void GetObjectData(SerializationInfo info, StreamingContext context)
GetType                   Method         System.Type GetType()
get_Attributes            Method         System.IO.FileAttributes get_Attributes()
get_CreationTime          Method         System.DateTime get_CreationTime()

get-member 显示您将其传输到其中的对象的成员。 您可以看到这些是 System.IO.DirectoryInfo 类的实际成员。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接