kazuakix の日記

Windows Phone とか好きです

PowerShell で static メソッドを呼び出す

先日某所で講師役をする機会がありました。本題はまったく別でしたが余談として とある値を PowerShell で計算してドヤ顔確認してもらっていました。

尚、参考にさせて頂いたページはコチラ。

「16進数だと ff になるんですね」で終わろうとしたところ...

PS C:\> (255).ToString("x")
ff

「2 進数は?」

え? あ、うん。さっきまで 2 進数で説明してたもんね...どうしよう? スルーするか? なんて考えてたのですが、ふとどこか*1[クラス名]::メソッド のような表記を見たような記憶が蘇ってこんな感じでタイプしてみました。

PS C:\> [System.Convert]::ToString

OverloadDefinitions
-------------------
static string ToString(System.Object value)
static string ToString(System.Object value, System.IFormatProvider provider)
static string ToString(bool value)
static string ToString(bool value, System.IFormatProvider provider)
static string ToString(char value)
static string ToString(char value, System.IFormatProvider provider)
static string ToString(sbyte value)
static string ToString(sbyte value, System.IFormatProvider provider)
static string ToString(byte value)
static string ToString(byte value, System.IFormatProvider provider)
static string ToString(int16 value)
static string ToString(int16 value, System.IFormatProvider provider)
static string ToString(uint16 value)
static string ToString(uint16 value, System.IFormatProvider provider)
static string ToString(int value)
static string ToString(int value, System.IFormatProvider provider)
static string ToString(uint32 value)
static string ToString(uint32 value, System.IFormatProvider provider)
static string ToString(long value)
static string ToString(long value, System.IFormatProvider provider)
static string ToString(uint64 value)
static string ToString(uint64 value, System.IFormatProvider provider)
static string ToString(float value)
static string ToString(float value, System.IFormatProvider provider)
static string ToString(double value)
static string ToString(double value, System.IFormatProvider provider)
static string ToString(decimal value)
static string ToString(decimal value, System.IFormatProvider provider)
static string ToString(datetime value)
static string ToString(datetime value, System.IFormatProvider provider)
static string ToString(string value)
static string ToString(string value, System.IFormatProvider provider)
static string ToString(byte value, int toBase)
static string ToString(int16 value, int toBase)
static string ToString(int value, int toBase)
static string ToString(long value, int toBase)

なんか出てきた! (ここまで 10 秒ほど)

「そうですね、.NET にこんなクラスがあってですね」と言いながらタイプし直します。

PS C:\> [System.Convert]::ToString(255, 2)
11111111

心の中でガッツポーズしながら、説明を続けましたとさ。めでたしめでたし。


後から調べたら 2 進数から 10 進数はこんな感じでした。

[System.Convert]::ToInt32("11111111",2)


その他の static メソッド(メンバ)一覧はこんな感じで確認できます。
ついでに [] で呼び出すときは System. が省略できるようです。

PS C:\Users\kazuaki> [Convert] | Get-Member -static


   TypeName: System.Convert

Name                MemberType Definition
----                ---------- ----------
ChangeType          Method     static System.Object ChangeType(System.Object value, System.TypeCode typeCode), stati...
Equals              Method     static bool Equals(System.Object objA, System.Object objB)
FromBase64CharArray Method     static byte[] FromBase64CharArray(char[] inArray, int offset, int length)
FromBase64String    Method     static byte[] FromBase64String(string s)
GetTypeCode         Method     static System.TypeCode GetTypeCode(System.Object value)
IsDBNull            Method     static bool IsDBNull(System.Object value)
ReferenceEquals     Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
ToBase64CharArray   Method     static int ToBase64CharArray(byte[] inArray, int offsetIn, int length, char[] outArra...
ToBase64String      Method     static string ToBase64String(byte[] inArray), static string ToBase64String(byte[] inA...
ToBoolean           Method     static bool ToBoolean(System.Object value), static bool ToBoolean(System.Object value...
ToByte              Method     static byte ToByte(System.Object value), static byte ToByte(System.Object value, Syst...
ToChar              Method     static char ToChar(System.Object value), static char ToChar(System.Object value, Syst...
ToDateTime          Method     static datetime ToDateTime(datetime value), static datetime ToDateTime(System.Object ...
ToDecimal           Method     static decimal ToDecimal(System.Object value), static decimal ToDecimal(System.Object...
ToDouble            Method     static double ToDouble(System.Object value), static double ToDouble(System.Object val...
ToInt16             Method     static int16 ToInt16(System.Object value), static int16 ToInt16(System.Object value, ...
ToInt32             Method     static int ToInt32(System.Object value), static int ToInt32(System.Object value, Syst...
ToInt64             Method     static long ToInt64(System.Object value), static long ToInt64(System.Object value, Sy...
ToSByte             Method     static sbyte ToSByte(System.Object value), static sbyte ToSByte(System.Object value, ...
ToSingle            Method     static float ToSingle(System.Object value), static float ToSingle(System.Object value...
ToString            Method     static string ToString(System.Object value), static string ToString(System.Object val...
ToUInt16            Method     static uint16 ToUInt16(System.Object value), static uint16 ToUInt16(System.Object val...
ToUInt32            Method     static uint32 ToUInt32(System.Object value), static uint32 ToUInt32(System.Object val...
ToUInt64            Method     static uint64 ToUInt64(System.Object value), static uint64 ToUInt64(System.Object val...
DBNull              Property   static System.Object DBNull {get;}

強烈な体験で脳に刻み込まれた感じ。

*1:たぶん PowerShell 勉強会