是否可以在Powershell中指定要从中使用New-Object的dll? [英] Can I specify a dll from which to use New-Object in Powershell?

查看:33
本文介绍了是否可以在Powershell中指定要从中使用New-Object的dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要 New-Object 一个类型,但是有多个具有相同名称和名称空间的类型.我找不到从 New-Object 决定如何从多个位置存在的dll中创建类型的类型.是否可以指定?理想情况下,我想将dll名称传递给 New-Object ,但似乎没有为此选择.

I have a scenario where by I need to New-Object a type but there are multiple types with the same name and namespace. I can't find how New-Object decides which dll to create the type from when it exists in multiple places. Is it possible to specify? Ideally I'd like to pass the dll name to the New-Object but there doesn't appear to be an option for this.

推荐答案

指定完整的这也适用于类型文字:

[MyType,AssemblyName,Version=1.0.0.0,Culture=neutral,PublicKeyToken=abcdef0123456789]::new()


您可以使用 AssemblyName.GetAssemblyName()从磁盘上轻松获取程序集的标准名称:


You can easily obtain the fully qualified name of an assembly from disk with AssemblyName.GetAssemblyName():

# Load the assembly
$AssemblyPath = '.\MyAssembly.dll'
Add-Type -Path $AssemblyPath

# Fetch the assembly's name
$AssemblyName = [System.Reflection.AssemblyName]::GetAssemblyName((Resolve-Path $AssemblyPath).Path)

# Construct full type name
$TypeName = 'MyType'
$AssemblyQualifiedTypeName = $TypeName,$AssemblyName -join ', '

# Instantiate and object of this type
$MyObject = New-Object -TypeName $TypeName
#or
$MyObject = ($TypeName -as [Type])::new()

这篇关于是否可以在Powershell中指定要从中使用New-Object的dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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