使用反射处理依赖项
我正在尝试读取已知路径中一个 dll 文件的所有依赖项。为此,我使用反射,如以下代码所示:
For Each AsmName As AssemblyName In asm.GetReferencedAssemblies()
Try
Log += AsmName.Name & " : " & [Assembly].ReflectionOnlyLoad(AsmName.ToString).Location & vbCrLf
Catch ex As Exception
Log += AsmName.Name & " : NOT FOUND." & vbCrLf
End Try
Next
为了测试这一点,我在 VB NET 中开发了一个 dll,其中包含一些我制作的其他 dll 文件的依赖项。
当我传递文件路径时,我可以检测依赖于该文件的所有程序集。但是当我尝试恢复依赖项的文件路径时,我可以找到除我制作的 dll 文件之外的所有依赖项的所有文件路径。
当我运行代码时,我获得以下日志结果:
mscorlib : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll Microsoft.VisualBasic:C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.VisualBasic \ 8.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualBasic.dll System.Xml.Linq:C:\ WINDOWS \ assembly \ GAC_MSIL \ System.Xml.Linq \ 3.5.0.0__b77a5c561934e089 \ System.Xml.Linq.dll 系统:C:\ WINDOWS \ assembly \ GAC_MSIL \ System \ 2.0.0.0__b77a5c561934e089 \ System.dll IM2_DataFile:未找到。 系统数据:C:\ WINDOWS \ assembly \ GAC_32 \ System.Data \ 2.0.0.0__b77a5c561934e089 \ System.Data.dll IM2_DataRow:未找到。 系统绘图:C:\ WINDOWS \ assembly \ GAC_MSIL \ System.Drawing \ 2.0.0.0__b03f5f7f11d50a3a \ System.Drawing.dll IM2_DataProviderTools:未找到。 系统.Windows.Forms:C:\ WINDOWS \ assembly \ GAC_MSIL \ System.Windows.Forms \ 2.0.0.0__b77a5c561934e089 \ System.Windows.Forms.dll Telerik.WinControls.UI:C:\ WINDOWS \ assembly \ GAC_MSIL \ Telerik.WinControls.UI \ 2009.2.9.729__5bb2a467cbec794e \ Telerik.WinControls.UI.dll IM2_DataRowPanel:未找到。 Telerik.WinControls.GridView:C:\ WINDOWS \ assembly \ GAC_MSIL \ Telerik.WinControls.GridView \ 2009.2.9.729__5bb2a467cbec794e \ Telerik.WinControls.GridView.dll IM2_Language_Controller:未找到。 系统核心:C:\ WINDOWS \ assembly \ GAC_MSIL \ System.Core \ 3.5.0.0__b77a5c561934e089 \ System.Core.dll Telerik.WinControls:C:\ WINDOWS \ assembly \ GAC_MSIL \ Telerik.WinControls \ 2009.2.9.729__5bb2a467cbec794e \ Telerik.WinControls.dll IM2_CommonTools:未找到。
所有以 IM2_ 开头的文件都是我想要获取路径的文件。
我发现找不到的文件具有 PublicKeyToken=null。我用谷歌搜索过这个,这是因为我在编译时没有序列化这些类。
请问有人可以帮助我吗?目标是获取 dll 文件的所有依赖项的所有文件路径。
提前致谢。
I'm trying to read all dependencies of one dll file in a known path. To do this I'm using reflection as this code shows:
For Each AsmName As AssemblyName In asm.GetReferencedAssemblies()
Try
Log += AsmName.Name & " : " & [Assembly].ReflectionOnlyLoad(AsmName.ToString).Location & vbCrLf
Catch ex As Exception
Log += AsmName.Name & " : NOT FOUND." & vbCrLf
End Try
Next
To test this I've developed a dll in VB NET with a few dependeces from other dll files made by me.
When I pass the file path, I can detect all assemblies which depends the file. But when I try to recover the file path of the dependencie I can find all file path for all dependencies excepts the dll files made by me.
When I run the code I'm obtaining the following log result:
mscorlib : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll
Microsoft.VisualBasic : C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualBasic\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll
System.Xml.Linq : C:\WINDOWS\assembly\GAC_MSIL\System.Xml.Linq\3.5.0.0__b77a5c561934e089\System.Xml.Linq.dll
System : C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll
IM2_DataFile : NOT FOUND.
System.Data : C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll
IM2_DataRow : NOT FOUND.
System.Drawing : C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll
IM2_DataProviderTools : NOT FOUND.
System.Windows.Forms : C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll
Telerik.WinControls.UI : C:\WINDOWS\assembly\GAC_MSIL\Telerik.WinControls.UI\2009.2.9.729__5bb2a467cbec794e\Telerik.WinControls.UI.dll
IM2_DataRowPanel : NOT FOUND.
Telerik.WinControls.GridView : C:\WINDOWS\assembly\GAC_MSIL\Telerik.WinControls.GridView\2009.2.9.729__5bb2a467cbec794e\Telerik.WinControls.GridView.dll
IM2_Language_Controller : NOT FOUND.
System.Core : C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll
Telerik.WinControls : C:\WINDOWS\assembly\GAC_MSIL\Telerik.WinControls\2009.2.9.729__5bb2a467cbec794e\Telerik.WinControls.dll
IM2_CommonTools : NOT FOUND.
All files that starts by IM2_ are the files what i want to get the path.
I've observed that the files wich can't find has PublicKeyToken=null. I've googled by this and it's because I didn't serializaed the classes when were compiled.
Please could anyone help me? The target is get all filepath of all dependencies of a dll file.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试使用程序集名称(而不是程序集的完整文件/路径)来加载程序集。
对于在 GAC(全局程序集缓存)中注册的程序集,这可以工作,但是+您的+程序集可能未在 GAC 中注册,因此无法以相同的方式加载它们。
我将不得不深入研究如何通过实际文件名加载程序集的示例。
It looks like you're trying to load the assemblies using on the assembly name (NOT the full file/path to the assembly).
For assemblies registered in the GAC (Global Assembly cache) that'll work, but +your+ assemblies probably aren't registered in the GAC, so they can't be loaded in the same way.
I'll have to dig around for an example of how to load an assembly via an actual filename.