等效于powershell中的c#的“使用”关键字?
-
20-08-2019 - |
题
当我在C#中的.NET-FRAMEWORK中使用另一个对象时,我可以通过使用该指令来节省大量键入。
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-poor-man-man-s-using-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-member(以结构化的方式)将它们添加到我关心的对象中。
另请参阅此后续帖子: 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#指令,但至少非常容易使用。
感谢大家的投入。我将理查德·伯格(Richard Berg)的贡献标记为答案,因为它最类似于我想要的东西。
您所有的答案都使我走上了最有前途的赛道: 他的博客文章 基思·达比(Keith Dahlby)提出了一个get-type Commandlet,可以轻松批准用于通用方法的类型。
我认为没有理由不排除这一点,也可以通过一种类型的预定义的组件搜索。
免责声明:我还没有建造 - 还没有建立...
这是可以使用它的方式:
$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的一个不错的通用列表。当然,我将在其他实用程序函数中包裹起来。扩展的Get-type将包括一个步骤,以再次解决任何给定类型的路径。
#Requires -Version 5
using namespace System.Management.Automation.Host
#using module
我意识到这是一个古老的帖子,但是我正在寻找同一件事,并遇到了这一点: http://weblogs.asp.net/adweigert/powershell-adding-the-using-statement
编辑:我想我应该指定它允许您使用...的熟悉语法
using ($x = $y) { ... }