我一直在尝试将自定义DLL加载到PowerShell中,但没有运气。
正在创建我的DLL based on this repository。我的最终目标是能够加载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(或更低版本)为目标。