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

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

本文介绍了如何检查程序集是否引用了另一个程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 assembly = //some assembly
System.Reflection.AssemblyName[] names = assembly.GetReferencedAssemblies();



<code> System.Reflection.AssemblyName带有程序集的完整标识,包括其版本,请参见 [ ^ ].

您可以使用"=="运算符来比较两个程序集名称.如果程序集已签名,则将通过其世界唯一名称对其进行比较.如果需要知道引用的程序集的路径名,请检查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 http://msdn.microsoft.com/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


这篇关于如何检查程序集是否引用了另一个程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:09