ここで、Powershell に拡張メソッドを追加するための Bart de Smet のソリューションを実装しています。
http://bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx
それはうまくいきます!ほとんど!彼はジェネリックを除外していますが、それは暗黒時代 (2007 年) にさかのぼります。そのため、今日の Powershell 3.0 でそれが可能かどうかを調べようとしています。これが私がやろうとしていることの簡単な例です:
$ls = new-object collections.generic.list[string]
'generic'
update-typedata -force -typename collections.generic.list`1 `
-membertype scriptmethod -membername test -value {'test!'}
$ls.test() # fail
'string'
update-typedata -force -typename collections.generic.list[string] `
-membertype scriptmethod -membername test -value {'test!'}
$ls.test() # works!
これは以下を出力します:
generic
Method invocation failed because [System.Collections.Generic.List`1[[System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] doesn't contain a method
named 'test'.
At C:\Temp\blah5.ps1:12 char:1
+ $ls.test()
+ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
string
test!
これで、Powershell はジェネリック型定義を操作できるようになりました。typedataシステムと統合されていないようです...
それとも私はそれを間違っていますか?これを機能させるために考えられる方法はありますか?