【问题标题】:Does PowerShell support OOP?PowerShell 是否支持 OOP?
【发布时间】:2018-02-19 06:19:53
【问题描述】:

PowerShell 中的类、接口、Mixin 等概念是什么?支持面向对象吗?如果是这样,我在哪里可以阅读到这方面的信息?

【问题讨论】:

    标签: oop powershell


    【解决方案1】:

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

    详细说明

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

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

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

    help Add-Type 了解更多信息。

    另见:

    【讨论】:

      【解决方案2】:

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

      Add-Type 很有用,但如果您必须转义到 C# 或 VB 来定义一个类或实现特定接口的类,我不认为第一类支持创建类/接口。

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

      【讨论】:

      • 很好的答案,很好的书。谢谢你们!
      【解决方案3】:

      Powershell 5 似乎支持一些主流的 OOP。

      所有功劳归于这个人: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"
          }
      }
      

      【讨论】:

      【解决方案4】:

      PowerShell 管道处理对象,而不仅仅是 Unix 管道处理的文本流。所有变量也是对象的实例。顺便说一句,这些都是 .NET 对象。

      以下是通过管道传送到 get-member cmdlet 的“ls”命令的部分输出:

          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 类的实际成员。

      【讨论】:

        猜你喜欢
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 2020-06-22
        • 2015-10-29
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        相关资源
        最近更新 更多