PowerShell-为加载的程序集设置别名 [英] PowerShell - Set Alias for Loaded Assembly

查看:68
本文介绍了PowerShell-为加载的程序集设置别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将 .Net 程序集加载到PowerShell:

I use this code to load a .Net assembly to PowerShell:

[System.Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | out-null 
[System.Windows.Forms.MessageBox]::Show("Hello world")

我可以为程序集设置别名(例如:'System.Windows.Forms'='Forms'),以便在调用诸如MessageBox.Show()?

Can I set an alias for an assembly (for example: 'System.Windows.Forms' = 'Forms') so that I don't have to type the assembly's full name when calling a static methods like MessageBox.Show()?

推荐答案

虽然您本身无法创建某种名称空间别名,但是可以使用以下技巧(摘自Lee Holmes的PowerShell手册):

While you can't create some sort of namespace alias per se, you can use the following trick (taken from Lee Holmes' PowerShell Cookbook):

$namespace = "System.Windows.Forms.{0}"
$form = New-Object ($namespace -f "Form")

但是,这仅适用于 New-Object ,因为这需要一个字符串作为类名.您不能在方括号中的类型名称中使用该语法.

But that only will work with New-Object since that takes a string for the class name. You can't use that syntax with a type name in square brackets.

可以做的是,省去了暗示的 System 部分:

What you can do, however, is leave out the System part which is implied:

[Windows.Forms.MessageBox]::Show("Hello World!")

使它短一些.

这篇关于PowerShell-为加载的程序集设置别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆