当前位置: 首页 > 编程笔记 >

Powershell中可以使用的.Net实用静态方法

席兴朝
2023-03-14
本文向大家介绍Powershell中可以使用的.Net实用静态方法,包括了Powershell中可以使用的.Net实用静态方法的使用技巧和注意事项,需要的朋友参考一下

支持所有版本。

Powershell能够使用.net静态方法,比如下面有一些你常用的方法:


[Math]::Round(7.9)

 

[Convert]::ToString(576255753217, 8)

 

[Guid]::NewGuid()

 

[Net.Dns]::GetHostByName('schulung12')

 

[IO.Path]::GetExtension('c:\test.txt')

 

[IO.Path]::ChangeExtension('c:\test.txt', 'bak')

要获得更多的方法,你可以删掉方括号后面的代码,再添加两个冒号,这时在Powershell_ISE中将会弹出这个类所有可选的方法及属性。在Powershell控制台中,你也可以使用TAB来获取选择方法属性。

当然你也可以通过管道符查看他所有类型:


PS> [Math] | Get-Member -MemberType *Method -Static

   TypeName: System.Math

Name            MemberType Definition                                                                                     ----            ---------- ----------                                                                                     Abs             Method     static sbyte Abs(sbyte value), static int16 Abs(int16 value), static int Abs(int value), sta... Acos            Method     static double Acos(double d)           Asin            Method     static double Asin(double d)            Atan            Method     static double Atan(double d)            Atan2           Method     static double Atan2(double y, double x) BigMul          Method     static long BigMul(int a, int b)   Ceiling         Method     static decimal Ceiling(decimal d), static double Ceiling(double a) Cos             Method     static double Cos(double d) Cosh            Method     static double Cosh(double value                                                             DivRem          Method     static int DivRem(int a, int b, [ref] int result), static long DivRem(long a, long b, [ref] ... Equals          Method     static bool Equals(System.Object objA, System.Object objB) Exp             Method     static double Exp(double d)              Floor           Method     static decimal Floor(decimal d), static double Floor(double d) IEEERemainder   Method     static double IEEERemainder(double x, double y) Log             Method     static double Log(double d), static double Log(double a, double newBase) Log10           Method     static double Log10(double d) Max             Method     static sbyte Max(sbyte val1, sbyte val2), static byte Max(byte val1, byte val2), static int1... Min             Method     static sbyte Min(sbyte val1, sbyte val2), static byte Min(byte val1, byte val2), static int1... Pow             Method     static double Pow(double x, double y) ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB) Round           Method     static double Round(double a), static double Round(double value, int digits), static double ... Sign            Method     static int Sign(sbyte value), static int Sign(int16 value), static int Sign(int value), stat... Sin             Method     static double Sin(double a) Sinh            Method     static double Sinh(double value)   Sqrt            Method     static double Sqrt(double d) Tan             Method     static double Tan(double a) Tanh            Method     static double Tanh(double value) Truncate        Method     static decimal Truncate(decimal d), static double Truncate(double d)

要查看方法的参数,省略掉他的括号即可:


PS> Get-Something -Path test

You entered test.

PS> [Math]::Round

OverloadDefinitions                                                                                                       -------------------                                                                                                       static double Round(double a static double Round(double value, int digits) static double Round(double value, System.MidpointRounding mode) static double Round(double value, int digits, System.MidpointRounding mode static decimal Round(decimal d static decimal Round(decimal d, int decimals) static decimal Round(decimal d, System.MidpointRounding mode) static decimal Round(decimal d, int decimals, System.MidpointRounding mode)


 类似资料:
  • 问题内容: 在Java中,创建了静态方法来访问它而没有任何对象实例。这对我来说很有意义。但是最近我碰到一个奇怪的事情,Java中的静态方法也可以通过其对象实例进行访问。这对我来说似乎很奇怪。你们谁知道 Java 为什么提供此功能?允许在没有实例的情况下访问静态方法有什么意义? 问题答案: 这样做的好处是,它允许您采用实例方法并将其转换为静态方法,而无需修改任何现有代码(除 类之外),从而实现向后兼

  • 本文向大家介绍PowerShell中调用.NET对象的静态方法、静态属性和类方法、类属性例子,包括了PowerShell中调用.NET对象的静态方法、静态属性和类方法、类属性例子的使用技巧和注意事项,需要的朋友参考一下 本文介绍在PowerShell中如何使用.NET对象,PowerShell内在支持大量的.NET对象。 调用类的静态方法 用中括号把类的名称括起来,然后输入两个冒号,然后再输入方法

  • 问题内容: 有什么方法可以使用静态字段。如果没有,还有其他方法可以做到这一点吗? 问题答案: 没有。你不能在Spring中自动连线或手动连线静态字段。为此,你必须编写自己的逻辑。

  • 问题内容: Python中是否可以有静态类变量或方法?为此需要什么语法? 问题答案: 在类定义内声明但在方法内声明的变量是类或静态变量: 正如@ millerdev指出的那样,这将创建一个类级别的i变量,但这不同于任何实例级别的i变量,因此你可以 这与C ++和Java不同,但与C#并没有太大区别,在C#中,无法使用对实例的引用来访问静态成员。

  • 问题内容: 尝试在静态类中调用非静态方法时遇到错误。 无法从类型播放中静态引用非静态方法methodName() 我不能使该方法静态,因为这也给我一个错误。 此静态方法无法从xInterface隐藏实例方法 有什么办法可以在另一个静态方法中轮回调用非静态方法?(这两种方法位于单独的包和单独的类中)。 问题答案: 从静态方法中调用非静态方法的唯一方法是使类的实例包含非静态方法。根据定义,非静态方法是