PowerShell直接調用.net靜態類和方法

不是全部 .NET Framework 類均可使用 New-Object 來建立。 例如,若是你嘗試使用 New-Object 建立 System.Environment 或 System.Math 對象,你將收到如下錯誤消息:shell

PS> New-Object System.Environment
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Environment.
At line:1 char:11
+ New-Object  <<<< System.Environment

PS> New-Object System.Math
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Math.
At line:1 char:11
+ New-Object  <<<< System.Math

之因此發生這些錯誤,是由於沒法從這些類建立新的對象。 這些類是不更改狀態的方法和屬性的引用庫。 你無需建立這些類,只須要使用它們。 這樣的類和方法稱爲靜態類,由於它們不會被建立、銷燬或更改。微信

System.Environment 類包含關於當前進程工做環境的通常信息,若是是在 Windows PowerShell 內工做,該進程爲 powershell.exe。app

若是嘗試經過鍵入 [System.Environment] | Get-Member 來查看此類的詳細信息,對象類型將報告爲 System.RuntimeType,而不是 System.Environment:ide

PS> [System.Environment] | Get-Member
   TypeName: System.RuntimeType

該類型的使用方式示例以下:.net

[System.Environment]::Commandline
[System.Environment]::OSVersion
[System.Environment]::HasShutdownStarted

另外咱們還可使用另一種靜態的類進行數學運算(System.Math)code

PS> [System.Math]::Sqrt(9)
3
PS> [System.Math]::Pow(2,3)
8
PS> [System.Math]::Floor(3.3)
3
PS> [System.Math]::Floor(-3.3)
-4
PS> [System.Math]::Ceiling(3.3)
4
PS> [System.Math]::Ceiling(-3.3)
-3
PS> [System.Math]::Max(2,7)
7
PS> [System.Math]::Min(2,7)
2
PS> [System.Math]::Truncate(9.3)
9
PS> [System.Math]::Truncate(-9.3)
-9

使用get-member能夠查看到該靜態類支持的運算方法:對象

PS> [System.Math] | Get-Member -Static -MemberType Methods

   TypeName: System.Math

Name            MemberType Definition
----            ---------- ----------
Abs             Method     static System.Single Abs(Single value), static Sy...
Acos            Method     static System.Double Acos(Double d)
Asin            Method     static System.Double Asin(Double d)
Atan            Method     static System.Double Atan(Double d)
Atan2           Method     static System.Double Atan2(Double y, Double x)
BigMul          Method     static System.Int64 BigMul(Int32 a, Int32 b)
Ceiling         Method     static System.Double Ceiling(Double a), static Sy...
Cos             Method     static System.Double Cos(Double d)
Cosh            Method     static System.Double Cosh(Double value)
DivRem          Method     static System.Int32 DivRem(Int32 a, Int32 b, Int3...
Equals          Method     static System.Boolean Equals(Object objA, Object ...
Exp             Method     static System.Double Exp(Double d)
Floor           Method     static System.Double Floor(Double d), static Syst...
IEEERemainder   Method     static System.Double IEEERemainder(Double x, Doub...
Log             Method     static System.Double Log(Double d), static System...
Log10           Method     static System.Double Log10(Double d)
Max             Method     static System.SByte Max(SByte val1, SByte val2), ...
Min             Method     static System.SByte Min(SByte val1, SByte val2), ...
Pow             Method     static System.Double Pow(Double x, Double y)
ReferenceEquals Method     static System.Boolean ReferenceEquals(Object objA...
Round           Method     static System.Double Round(Double a), static Syst...
Sign            Method     static System.Int32 Sign(SByte value), static Sys...
Sin             Method     static System.Double Sin(Double a)
Sinh            Method     static System.Double Sinh(Double value)
Sqrt            Method     static System.Double Sqrt(Double d)
Tan             Method     static System.Double Tan(Double a)
Tanh            Method     static System.Double Tanh(Double value)
Truncate        Method     static System.Decimal Truncate(Decimal d), static...

您也能夠關注下方的微信公衆號獲取更多內容
PowerShell直接調用.net靜態類和方法進程

相關文章
相關標籤/搜索