如何检查程序集是否引用了另一个程序集

编程入门 行业动态 更新时间:2024-10-27 21:13:03
本文介绍了如何检查程序集是否引用了另一个程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

C#中是否可以检查给定程序集是否引用了另一个程序集? 我有两个包含两个不同销售点设备(POS1和POS2)的API命令的dll. 我也有一些包含使用这些API的UI实现的dll. 我怎么知道哪个UI dll引用/使用特定的POS dll?这样做的目的是提供一个主UI,用户可以在其中选择要使用的POS dll,然后它将显示所选POS的可用UI实现. 谢谢

Is there a way in C# to check if a given assembly is referencing another assembly? I have two dlls containing the API commands of two different Point-Of-Sale device (POS1 and POS2). I also have a few dlls that contain the UI implementations of using those APIs. How can I know which UI dll references/uses the specific POS dll? The point of this is to present a main UI where the user can select which POS dll to use and then it will display the available UI implementations for the selected POS. Thanks

推荐答案

看看 System.Reflection.Assembly.GetReferencedAssemblies() [ ^ ].基本上,您将构造一个Assembly对象来表示要获取其引用的DLL,然后调用GetReferencedAssemblies方法,并查看是否有任何条目引用了特定的POS DLL. 注意:这仅适用于.NET程序集.不知道将使用哪种本机代码. Have a look at System.Reflection.Assembly.GetReferencedAssemblies()[^]. Basically you''ll construct an Assembly object to represent the DLL you want to get the references of, and then call the GetReferencedAssemblies method and see if any of the entries refer to the specific POS DLL. Note: this will only work for .NET assemblies. No idea what one would use for native code.

很容易.首先,让我们看看如何获​​取引用的程序集: Easy enough. First, let''s see how to get referenced assemblies: System.Reflection.Assembly assembly = //some assembly System.Reflection.AssemblyName[] names = assembly.GetReferencedAssemblies();

类<code> System.Reflection.AssemblyName带有程序集的完整标识,包括其版本,请参见 msdn.microsoft/en-us/library/system.reflection.assemblyname.aspx [ ^ ]. 您可以使用"=="运算符来比较两个程序集名称.如果程序集已签名,则将通过其世界唯一名称对其进行比较.如果需要知道引用的程序集的路径名,请检查AssemblyName.CodeBase.此属性显示程序集主可执行模块的路径名. .NET Framework和C#编译器支持由多个模块组成的程序集,但Visual Studio仅支持单模块程序集. (请勿将模块与引用的程序集混淆.模块是比程序集更细粒度的单元.) 自然,程序集文件不能不包含其他引用该文件的程序集的信息.您只能获取有关给定程序集引用的程序集的信息.当您需要知道哪些程序集引用给定程序集时,例如,可以在某个目录(可能在所有文件系统中)中收集所有可执行文件(EXE,DLL或您感兴趣的其他文件),检查是否是有效的程序集,然后使用System.Reflection.Assembly.GetReferencedAssemblies检查所有引用的程序集.拾取引用您给定装配的所有装配.可能是昂贵的操作:

The class <code>System.Reflection.AssemblyName carries full identity of the assembly, including its version, see msdn.microsoft/en-us/library/system.reflection.assemblyname.aspx[^]. You can compare two assembly names using "==" operator. If assemblies are signed, they are compared by their world-unique names. When you need to know the path name if the referenced assembly, check AssemblyName.CodeBase. This property shows the path name of assemblies main executable module. The .NET Framework and C# compiler supports assemblies composed of more that one module, but Visual Studio supports only single-module assemblies. (Do not mix up modules with referenced assemblies. Modules are more fine-grain units than assemblies.) Naturally, assembly file(s) cannot not contain information on other assemblies which reference it. You can only get information on assemblies referenced by a given assembly. As you need to know which assemblies reference your given assembly, you can, for example, collect all executable files (EXE, DLL or whatever else you''re interested in) in some directory (possible in all file system), examine if it is a valid assembly, then examine all referenced assemblies using System.Reflection.Assembly.GetReferencedAssemblies. Pick up all assemblies which reference you given assembly. It can be costly operation:

string[] GetReferencingAssemblies(string referencedAssembly, string[] executableModules) { string referencedAssemblyLo = referencedAssembly.ToLower(); System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>(); foreach (string executableModule in executableModules) { try { System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(executableModule); //may throw exception System.Reflection.AssemblyName[] names = assembly.GetReferencedAssemblies(); if (Array.FindIndex( names, new Predicate<system.reflection.assemblyname>((name) => { return name.CodeBase.ToLower() == referencedAssemblyLo; }))>=0) result.Add(executableModule); } catch { continue; } // caught exception means the module is not .NET assembly } //loop executableModules return result.ToArray(); } //GetReferencingAssemblies

您可以使用System.IO.Directory获取某些扩展名文件的路径名数组.小心!这不是那么容易.正确吗,请参阅: Directory.Get.Files搜索模式问题 [ ^ ].

—SA

You can get the array of path names of the files of certain extension using System.IO.Directory. Be careful! It''s not so easy. Do do you right, see: Directory.Get.Files search pattern problem[^].

—SA

更多推荐

如何检查程序集是否引用了另一个程序集

本文发布于:2023-11-13 11:51:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!