Sio*_*Sio 3 .net c# powershell
我有一个场景,我需要New-Object一个类型,但有多个具有相同名称和命名空间的类型。New-Object当它存在于多个地方时,我无法找到如何决定从哪个 dll 创建类型。可以指定吗?理想情况下,我想将 dll 名称传递给 ,New-Object但似乎没有这个选项。
指定完整的程序集限定类型名称:
New-Object -TypeName 'MyType, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcdef0123456789'
Run Code Online (Sandbox Code Playgroud)
这也适用于类型文字:
[MyType,AssemblyName,Version=1.0.0.0,Culture=neutral,PublicKeyToken=abcdef0123456789]::new()
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令轻松地从磁盘获取程序集的完全限定名称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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
855 次 |
| 最近记录: |