不是所有 .NET Framework 类都可使用 New-Object 来创建。 例如,如果你尝试使用 New-Object 创建 System.Environment 或 System.Math 对象,你将收到以下错误消息: ``` 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。 如果尝试通过键入 [System.Environment] | Get-Member 来查看此类的详细信息,对象类型将报告为 System.RuntimeType,而不是 System.Environment: ``` PS> [System.Environment] | Get-Member TypeName: System.RuntimeType ``` 该类型的使用方式示例如下: ``` [System.Environment]::Commandline [System.Environment]::OSVersion [System.Environment]::HasShutdownStarted ``` 另外我们还可以使用另外一种静态的类进行数学运算(System.Math) ``` 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... ``` 您也可以关注下方的微信公众号获取更多内容 ![](https://s1.51cto.com/images/blog/201905/09/c37cdefbe7131d31967750ba35fc626c.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)