PowerShellのC#の「使用」キーワードに相当しますか?
-
20-08-2019 - |
質問
C#の.NETフレームワークで別のオブジェクトを使用すると、使用指令を使用して多くのタイピングを保存できます。
using FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It;
...
var blurb = new Thingamabob();
...
それで、Powershellには似たようなことをする方法はありますか?私はたくさんの.NETオブジェクトにアクセスしています、そして、入力する必要がありません
$blurb = new-object FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It.Thingamabob;
いつも。
解決
PowerShell 5.0(WMF5またはWindows 10以降に含まれる)は、 using namespace
言語に構築します。スクリプトで使用することができます。
#Require -Version 5.0
using namespace FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It
$blurb = [Thingamabob]::new()
( #Require
最初の行の声明は使用する必要はありません using namespace
, 、しかし、それはスクリプトがPS 4.0以下で実行されるのを防ぎます using namespace
構文エラーです。)
他のヒント
そのような名前空間レベルには本当に何もありません。よく使用されているタイプを変数に割り当ててからインスタンス化します。
$thingtype = [FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It.Thingamabob];
$blurb = New-Object $thingtype.FullName
タイプが繰り返し使用されない場合、おそらくそれだけの価値はありませんが、私はそれがあなたができる最善だと思います。
数年前のこのブログ投稿をご覧ください: http://blogs.msdn.com/richardb/archive/2007/02/21/add-types-ps1-opoor-man-sing-for-powershell.aspx
ここは add-types.ps1
, 、その記事から抜粋:
param(
[string] $assemblyName = $(throw 'assemblyName is required'),
[object] $object
)
process {
if ($_) {
$object = $_
}
if (! $object) {
throw 'must pass an -object parameter or pipe one in'
}
# load the required dll
$assembly = [System.Reflection.Assembly]::LoadWithPartialName($assemblyName)
# add each type as a member property
$assembly.GetTypes() |
where {$_.ispublic -and !$_.IsSubclassOf( [Exception] ) -and $_.name -notmatch "event"} |
foreach {
# avoid error messages in case it already exists
if (! ($object | get-member $_.name)) {
add-member noteproperty $_.name $_ -inputobject $object
}
}
}
そして、それを使用するには:
RICBERG470> $tfs | add-types "Microsoft.TeamFoundation.VersionControl.Client"
RICBERG470> $itemSpec = new-object $tfs.itemspec("$/foo", $tfs.RecursionType::none)
基本的に私がしていることは、非自明のタイプのアセンブリをクロールし、Addメンバーを使用して(構造化された方法で)私が気にするオブジェクトに(構造化された方法で)「コンストラクター」を書くことです。
このフォローアップ投稿も参照してください。 http://richardberg.net/blog/?p=38
これは単なる冗談です、冗談...
$fullnames = New-Object ( [System.Collections.Generic.List``1].MakeGenericType( [String]) );
function using ( $name ) {
foreach ( $type in [Reflection.Assembly]::LoadWithPartialName($name).GetTypes() )
{
$fullnames.Add($type.fullname);
}
}
function new ( $name ) {
$fullname = $fullnames -like "*.$name";
return , (New-Object $fullname[0]);
}
using System.Windows.Forms
using FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It
$a = new button
$b = new Thingamabob
PowerShell 2.0で動作してタイプエイリアスを追加するコードを次に示します。しかし、問題は、それがスコープされていないことです。いくつかの追加の作業を使用すると、名前空間を「輸入する」ことができますが、これにより良いスタートを切ることができます。
##############################################################################
#.SYNOPSIS
# Add a type accelerator to the current session.
#
#.DESCRIPTION
# The Add-TypeAccelerator function allows you to add a simple type accelerator
# (like [regex]) for a longer type (like [System.Text.RegularExpressions.Regex]).
#
#.PARAMETER Name
# The short form accelerator should be just the name you want to use (without
# square brackets).
#
#.PARAMETER Type
# The type you want the accelerator to accelerate.
#
#.PARAMETER Force
# Overwrites any existing type alias.
#
#.EXAMPLE
# Add-TypeAccelerator List "System.Collections.Generic.List``1"
# $MyList = New-Object List[String]
##############################################################################
function Add-TypeAccelerator {
[CmdletBinding()]
param(
[Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[String[]]$Name,
[Parameter(Position=2,Mandatory=$true,ValueFromPipeline=$true)]
[Type]$Type,
[Parameter()]
[Switch]$Force
)
process {
$TypeAccelerators = [Type]::GetType('System.Management.Automation.TypeAccelerators')
foreach ($a in $Name) {
if ( $TypeAccelerators::Get.ContainsKey($a) ) {
if ( $Force ) {
$TypeAccelerators::Remove($a) | Out-Null
$TypeAccelerators::Add($a,$Type)
}
elseif ( $Type -ne $TypeAccelerators::Get[$a] ) {
Write-Error "$a is already mapped to $($TypeAccelerators::Get[$a])"
}
}
else {
$TypeAccelerators::Add($a, $Type)
}
}
}
}
タイプのインスタンスを作成する必要がある場合は、長い名前空間の名前を文字列に保存できます。
$st = "System.Text"
$sb = New-Object "$st.StringBuilder"
それほど強力ではありません using
C#の指令ですが、少なくとも非常に使いやすいです。
ご意見をお寄せいただきありがとうございます。リチャード・バーグの貢献を答えとしてマークしました。なぜなら、それは私が探しているものに最もよく似ているからです。
あなたのすべての答えが私をトラックにもたらしました。 彼のブログ投稿 Keith Dahlbyは、一般的な方法でタイプの簡単なコンセントを可能にするGet-Typeコマンドレットを提案しています。
これを追い払うことに反対する理由はないと思います。また、事前定義されたアセンブリのパスをタイプに検索します。
免責事項:私はそれを構築していません - まだ...
これがそれを使用する方法です:
$path = (System.Collections.Generic, FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It)
$type = get-type -Path $path List Thingamabob
$obj = new-object $type
$obj.GetType()
これにより、ThingAmabobの素晴らしい一般的なリストが生まれます。もちろん、別のユーティリティ関数のパス定義を除いて、私はEverthingを締めくくります。拡張されたget-typeには、特定のタイプを解決するためのステップが含まれます。
#Requires -Version 5
using namespace System.Management.Automation.Host
#using module
私はこれが古い投稿であることに気づきましたが、私は同じことを探していて、これに出会いました: http://weblogs.asp.net/adweigert/powershell-adding-the-sing-statement
編集:馴染みのある構文を使用できるようにすることを指定する必要があると思います...
using ($x = $y) { ... }