【发布时间】:2017-12-08 13:35:51
【问题描述】:
我正在尝试(但失败)将 DLL(不在 GAC 中)加载到 powershell。
DLL 是Microsoft.Diagnostics.Runtime (ClrMD) Nuget Package 的一部分 见the full documentation for Microsoft.Diagnostics.Runtime。
我无法将它加入 GAC 的原因是因为 DLL 不是使用强名称创建的(这就是 gacutil 所说的)
所以我已经尝试了以下所有选项..但无法使其工作..想知道是否有人有任何技巧:
$dllpath = somepath\Microsoft.Diagnostics.Runtime.0.9.170809.03\lib\net40\Microsoft.Diagnostics.Runtime.dll"
#LoadFile (this shouldnt work according to method documentation,.. it's just for inspection)
[System.Reflection.Assembly]::LoadFile($dllpath)
#LoadFrom
[System.Reflection.Assembly]::LoadFrom($dllpath)
#LoadwithPartialName .. this is deprecated
[reflection.assembly]::LoadWithPartialName( "Microsoft.Diagnostics.Runtime")
# add-type
add-type -path $dllpath
还有
$dllname = "Microsoft.Diagnostics.Runtime, Version=0.8.31.1, Culture=neutral, PublicKeyToken=null"
[System.Reflection.Assembly]::Load($dllname)
我使用的 dllname:
$dllpath = "somepath\Microsoft.Diagnostics.Runtime.0.9.170809.03\lib\net40\Microsoft.Diagnostics.Runtime.dll"
$dllname = [System.Reflection.AssemblyName]::GetAssemblyName($dllpath).Fullname
无论如何.... 在上述所有情况下,我都可以看到 DLL 在我的会话中被加载到当前的 appdomain 中:
[System.AppDomain]::CurrentDomain.GetAssemblies() | where-object -Filterscript {$_.Fullname -like "*Diagnostics*"}
但是当我尝试引用它时,它说“类型不可用”
例如
这应该可以工作..当我在智能感知之后键入双冒号时应该吐出属性/方法 但它因“找不到类型”而失败
[Microsoft.Diagnostics.Runtime]
这绝对可以。 afaik 这是一个静态方法 第一个变量是 processID,第二个变量是超时(秒) 但它因“找不到类型”而失败
[Microsoft.Diagnostics.Runtime]::DataTarget.AttachToProcess(7984,5000)
它显然可以与 C# 一起使用,但我现在真的不想学习它,因为它会在我理解它时耽误我的时间......但也许是必要的步骤......
【问题讨论】:
-
我很幸运的一件事是使用模块清单来加载 DLL 而不是反射或
Add-Type。我不知道它有什么不同,但确实如此。使用New-ModuleManifest并更改RequiredAssemblies = @()字段。您输入的路径与.psd1文件所在的位置相关。 -
最后我在引用程序集时出错了.... powershell 需要
[Microsoft.Diagnostics.Runtime.DataTarget]::AttachToProcess(7984,5000)而不是原来的[Microsoft.Diagnostics.Runtime]::DataTarget.AttachToProcess(7984,5000)
标签: .net powershell clrmd