Answered by:
Getting all Referenced Assemblies with Reflection

Question
-
So,
Reflection. Seems straight forward enough, and logically, I found with the complexity of our primary application, that it would be best to write an application that would investigate all referenced assemblies and enumerate them in order to help me find any circular references.
So, the primary application has some 56 references. Some of these are in the GAC, but at least half of them are other projects within the entire solution. So my "secondary" project here, uses an openfiledialog to allow me to choose an exe or dll, and then loads up the assembly.
I Load the assembly, then using "Assembly.GetReferencedAssemblies()" i iterate through the AssemblyName objects and load the referenced assembly. However, when I use that method to retrieve the Referenced assemblies, only get 19. Some from the GAC some are the compiled class assemblies, but either way, this GetReferencedAssemblies() method does NOT return ALL references.
How do I get ALL refereces of an assembly using reflection?
ThanksAssembly _asm = Assembly.LoadFile(fileName); AssemblyName[] assemblies = _asm.GetReferencedAssemblies(); foreach (AssemblyName name in assemblies.OrderBy(n => n.Name)) { FileInfo afi = files.FirstOrDefault(file => file.Name == String.Format("{0}{1}", name.Name, file.Extension)); if (afi != null) { Assembly asm = Assembly.LoadFile(afi.FullName); } } //Above does not work. I get 19 AssemblyName objects, not 56, //when the EXE i am passing in as the 'fileName' parameter //references some 56 assemblies. WHY?
Jaeden "Sifo Dyas" al'Raec Ruiner
"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.Thursday, August 4, 2011 4:35 PM
Answers
-
Hello Jaeden,
You may not see all the referenced assemblies because Assemby.GetReferencedAssemblies function returns only those assemblies, which are loaded in the memory. Though you have added 56 references, you are actually referencing only 19.
To confirm this behavior, you create a sample console application. Add a reference to some assembly say System.Xml (no problem if it is already there). Do not write any code other than your enumerating referenced assemblies something like below.
string str = string.Empty; AssemblyName[] assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); foreach (AssemblyName item in assemblies) { str += item.Name + Environment.NewLine; } MessageBox.Show(str)
Run your application and you will not see System.Xml listed ! Now, just add a statement that uses some xml element some thing like below.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
Again run the application and now you see System.Xml gets listed under list of referenced assemblies.
You know, I find this very useful. When you finish with your application, you can easily remove all unused references (no need of any external application to do that for you )
Hope this helps you.
Please mark this post as answer if it solved your problem. Happy Programming!Thursday, August 4, 2011 4:56 PM
All replies
-
Hello Jaeden,
You may not see all the referenced assemblies because Assemby.GetReferencedAssemblies function returns only those assemblies, which are loaded in the memory. Though you have added 56 references, you are actually referencing only 19.
To confirm this behavior, you create a sample console application. Add a reference to some assembly say System.Xml (no problem if it is already there). Do not write any code other than your enumerating referenced assemblies something like below.
string str = string.Empty; AssemblyName[] assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); foreach (AssemblyName item in assemblies) { str += item.Name + Environment.NewLine; } MessageBox.Show(str)
Run your application and you will not see System.Xml listed ! Now, just add a statement that uses some xml element some thing like below.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
Again run the application and now you see System.Xml gets listed under list of referenced assemblies.
You know, I find this very useful. When you finish with your application, you can easily remove all unused references (no need of any external application to do that for you )
Hope this helps you.
Please mark this post as answer if it solved your problem. Happy Programming!Thursday, August 4, 2011 4:56 PM -
You may not see all the referenced assemblies because Assemby.GetReferencedAssemblies function returns only those assemblies, which are loaded in the memory. Though you have added 56 references, you are actually referencing only 19.
assemblies, which are loaded in the memory.
That is not actually true, since GetReferencedAssemblies() returns all referenced assemblies even if they haven't been loaded. The point is that compiler removes all the referencies that are not used in the code.
Wednesday, June 13, 2012 6:52 AM