我想使用 Add-Type 在 PS 脚本中执行 C# 代码。但我收到:
Metadata file 'Microsoft.Azure.ServiceBus.dll' could not be found
。C# 代码使用名为“Microsoft.Azure.ServiceBus”的 NuGet 包。
我引用的程序集似乎不起作用。这是我的代码:
附言
$cmd = {
$Assem = (
"Microsoft.Azure.ServiceBus",
"netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"
)
$Source = (Get-Content -Path "D:\myPath\csharp-code.cs" -ReadCount 0) -join "`n"
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
[ABC.Azure.ServiceBus.TopicSubscriber]::test()
}
# Execute script as Job to avoid "Add-Type"-conflict during development changes
$j = Start-Job -ScriptBlock $cmd
do
{
Receive-Job -Job $j
} while ( $j.State -eq "Running" )
C#
using Microsoft.Azure.ServiceBus;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace ABC.Azure.ServiceBus
{
class TopicSubscriber
{
bool test()
{
<more code>
}
}
}
这个问题的根源是什么,我该如何解决?不幸的是,我对 PS 和 C# 都不是很熟悉。