如何检查程序集是否引用了另一个程序集 [英] How to check if an assembly references another assembly

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

问题描述

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

推荐答案

看看 ^ ].基本上,您将构造一个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带有程序集的完整标识,包括其版本,请参见



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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆