簡體   English   中英

如何將自定義 DLL 加載到 PowerShell

[英]How to load a custom DLL into PowerShell

我一直在嘗試將自定義 DLL 加載到 PowerShell 中,但沒有成功。

我的 DLL 是基於此存儲庫創建 我的最終目標是能夠加載Monster.dll但我想我會從Dice.dll開始(因為它是一個幾乎沒有依賴項的靜態類)。

我試過的

在 PowerShell 中:

cd C:\\some\\repo\\dnd\\dice\\bin\\Debug\\netstandard2.1

$path = (pwd).Path

$path += "\\Dice.dll"

[Reflection.Assembly]::LoadFile("$path")

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\some\repo\dnd\dice\bin\Debug\netstandard2.1\Dice.dll

然后我嘗試調用簡單的 Roll Dice 函數(看起來像這樣)

[Dice.Dice]::Roll(20, 1)

它返回此錯誤:

Unable to find type [Dice.Dice].
At line:1 char:1
+ [Dice.Dice]::Roll(20, 1)
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Dice.Dice:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

如果我嘗試調用,也會發生同樣的事情:

[Dice]::Roll(20, 1)

我究竟做錯了什么?

編輯

有人建議我嘗試:

Add-Type -Path "$path"

並返回此錯誤:

Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
At line:1 char:1
+ Add-Type -Path "$path"
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
    + FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

真正的“LoadExeption”是

Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its
dependencies. The system cannot find the file specified.

評論中的另一個建議是運行:

[appdomain]::currentdomain.getassemblies()

我能夠從該命令中找到這些數據

@{CodeBase=file:///C:/some/Repo/DnD/dice/bin/Debug/netstandard2.1/Dice.dll; FullName=Dice,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null; EntryPoint=; DefinedTypes=; Evidence=System.Security.Policy.Evidence;
PermissionSet=<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
; SecurityRuleSet=Level2; ManifestModule=Dice.dll; ReflectionOnly=False;
Location=C:\some\Repo\DnD\dice\bin\Debug\netstandard2.1\Dice.dll; ImageRuntimeVersion=v4.0.30319;
GlobalAssemblyCache=False; HostContext=0; IsDynamic=False;
EscapedCodeBase=file:///C:/some/Repo/DnD/dice/bin/Debug/netstandard2.1/Dice.dll; ExportedTypes=;
IsFullyTrusted=True; CustomAttributes=; Modules=System.Reflection.RuntimeModule[]}

我也運行了這個命令:

try {
    ([appdomain]::currentdomain.GetAssemblies() | where Location -match 'dice').gettypes()
} catch {
    $_ | select *
}
PSMessageDetails      :
Exception             : System.Management.Automation.MethodInvocationException: Exception calling "GetTypes" with "0" argument(s):
                        "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more
                        information." ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested
                        types. Retrieve the LoaderExceptions property for more information.
                           at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
                           at System.Reflection.Assembly.GetTypes()
                           at CallSite.Target(Closure , CallSite , Object )
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,
                        Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          :
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

關鍵信息在“LoadException”消息中:

Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its
dependencies. The system cannot find the file specified.

它說無法找到程序集netstandard, Version=2.1.0.0 這會阻止 PowerShell 加載 Dice,因為 Dice 是針對 .NET Standard 2.1 編譯的。

我不確定通過 PowerShell 支持 .NET Standard 2.1 的細微差別。 您可能需要更新版本的 PowerShell,或者您可能只需要安裝正確的 .NET SDK/運行時組件。

如果您未綁定到 .NET Standard 2.1,請嘗試重新配置 C# 項目以面向 .NET Standard 2.0 或 .NET Framework 4.7(或更低版本)。

我想發布我自己的答案,因為我想幫助其他人解決這個問題。

如果您收到這樣的錯誤,請確保按照以下步驟操作:

  • 升級到最新版本的 .NET Core
  • 將 PowerShell 升級到可用的最新版本。 它必須支持您使用的 .NET Core 版本
  • 調用方法時,使用 PassThru 參數獲取更詳細的錯誤

Add-Type -AssemblyName "C:\\path\\to\\file.dll" -PassThru

同樣,我的問題的根源源於在嘗試使用 .NET Core 時使用PowerShell 5 如果您查看 PowerShell 的安裝位置,它包含自己的 .NET Core DLL,這與您的全局安裝不同。

我最終安裝了 PowerShell 7 Preview,因為(在撰寫本文時)它是唯一支持 .NET Core 3 的版本。*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM