Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
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
Posted
Updated 21-May-11 4:03am
v2

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.
 
Share this answer
 
Comments
01xedoc 22-May-11 6:51am    
Thanks! fortunately, all the assemblies are made in .net. :)
Easy enough. First, let's see how to get referenced assemblies:

C#
System.Reflection.Assembly assembly = //some assembly
System.Reflection.AssemblyName[] names = 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:

C#
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


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
 
Share this answer
 
v3
Comments
01xedoc 22-May-11 6:54am    
thanks SAKryukov! although it takes a while, im sure i can fit this in during the startup of the main app.
Sergey Alexandrovich Kryukov 22-May-11 13:45pm    
At least you have enough information to do all the check you need. You may also need to maintain certain versioning strategy and have a tool to check up the assembly version -- can be important.

Thanks for accepting this answer.
Good luck, call again.
--SA
Espen Harlinn 22-May-11 10:44am    
Good effort, my 5
Sergey Alexandrovich Kryukov 22-May-11 13:43pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900