Share via


GetReferencedAssemblies() not returning complete list

Question

Wednesday, December 16, 2009 9:33 AM

I want to get all the referenced assemblies in my executable and i'm using below given code... Unfiltered list returns total 18 assemblies and filtered ones (the one which i'm looking for shows 5).. Both results are not correct.. More then 10 assemblies are still missing in this list..

This is a multiple project solution and what i want is that i can get all the req assemblies list when the application is deployed on client - PROGRAMATCIALLY.. Can anybody help in this..

AssemblyName[] s = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); 
            var MyLibs = new List<string>();
            foreach (AssemblyName a in s)
            {

                if (a.Name.ToLower().StartsWith("myname"))
                {
                    MyLibs.Add(a.Name);
                }
            }

IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.

All replies (15)

Sunday, December 20, 2009 5:12 PM ✅Answered

Hi,

 I don't think there's a way to get each assembly your program needs because assemblies get loaded in the AppDomain "On Demand" meaning when they are first needed. 

I'd say, If you're running in the GAC, you're out of luck...  If you are running in an application folder, then simply iterate through the list of dlls located in the same directory as your EXE...  Then you'd only be missing the .net framework assemblies located in the GAC...

Hope this helps...


Monday, December 21, 2009 9:16 AM ✅Answered | 1 vote

Your problem is that if your code doesn't explicitly call the code inside some of the assemblies (or doesn't declare a variable of a type defined in the assembly), that assembly will be optimized out at the compilation time, regardless of whether you have referenced it or not.
The method GetReferencedAssemblies() returns the optimized list.
So, as a workaround, please create a method in the application which references at least one of the variables inside each of the assemblies.

void referenceAssemblies()
{
    Assembly1.Class1 dummy1 = new Assembly1.Class1();
    Assembly2.Class2 dummy2 = new Assembly2.Class2();
    // ... and so on, for all the assemblies.
}

This must help.


Wednesday, December 16, 2009 9:45 AM

try this:

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                
            }

and then filter on Assembly.FullName


Wednesday, December 16, 2009 9:55 AM

Ok.. tried this.. total list increased from 18 to 24 but result is still same.. 10+ are still missingIMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.


Wednesday, December 16, 2009 10:05 AM

Then im out of tricks, are u sure the assemblies are referenced to the project u use to execute this code?


Wednesday, December 16, 2009 10:16 AM

Yes, 100%.. I can clearly see all the references added in project..

The code is also being executed from that same project (which also happens to be the main project of this solution)IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.


Wednesday, December 16, 2009 10:19 AM

Are you sure, you use referenced assemblies in your project?
If you don't use a referenced assembly in your project then it will not compile with your project.With best regards, Yasser Zamani


Wednesday, December 16, 2009 10:37 AM

This is 100% confirm.. after this post only I have checked it 3 times again..

Project clearly references the assemblies, (i can send snapshot also) for which i want the list in code..

i chkd even in notepad by opening project file and i can clearly see refernces of assemblies..

If you are so sure that it shud return right result then I think this is a bug in GetReferencedAssemblies()

IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.


Wednesday, December 16, 2009 10:40 AM

What Yasser means is: are u using any of the classes, methods or other definitions from the referenced assemblies? If you are not, then the assembly will not get loaded even if u have referenced it in your project.


Wednesday, December 16, 2009 10:41 AM

Further to my previous msg, im also posting partial contents of project file.. out of these only 5 are being returned with the code which is incorrect..

    <Reference Include="Syncfusion.Compression.Base, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Core, Version=7.403.0.20, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.DocIO.Base, Version=7.303.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Grid.Base, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Grid.Grouping.Base, Version=7.403.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Grid.Grouping.Windows, Version=7.403.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Grid.Windows, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Grouping.Base, Version=7.403.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Grouping.Windows, Version=7.403.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Shared.Base, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Shared.Windows, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Tools.Base, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.Tools.Windows, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
    <Reference Include="Syncfusion.XlsIO.Base, Version=7.203.0.20, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>
IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.


Wednesday, December 16, 2009 10:49 AM

No, i don't mean this XML file. i mean as M. Dirksma said, do you use any object which defined in your referenced assemblies in your project?
If you don't use any object from referenced assembly in your project then it will not compile with your project.
With best regards, Yasser Zamani


Wednesday, December 16, 2009 11:00 AM

Yes definitely i'm using.. that is why i want the list.. if these dll files are missing then my application will fail..

These are 3rd part library files for generating xls and word files plus there are winform controls also grid, grouping grid and other controls..

I'm using these throughout the program..IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.


Wednesday, December 16, 2009 11:18 AM

To summarize this complete issue :-

  1. This is a multi project solution

  2. Main project from this code is being executed include references of all the req dll's..

  3. Bcos copy local to true is set for all 3rd part libs, when i compile application all req files are placed in release folder.. If i copy that release folder on other computer and run then app will run.. If i del any dll files in that folder then app will fail..

Req :- What i want is names of all those dll files which are auto copied in that release folder on which app is dependent..IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.


Wednesday, December 23, 2009 5:16 AM

Doing so will sure get all directly referenced assemblies but you must not forget the assemblies called by the other assemblies which you might not be able to call directly because you do not reference them directly... and probably for a good reason...

Also calling each assembly will slow your application startup time and "eager load" all assemblies which might be useless for some users.

I must agree tho that if you wanted to have all your referenced assemblies loaded into your appdomain and then returned by "GetReferencedAssemblies", this would do the trick...  Also it's rather static when this type of thing should be dynamic....

I'm pretty sure you could read the manifest of the main assembly (most likely via reflection) and find the LIST of assemblies the main refers (which are all the directly referenced assemblies) and then recursively iterate each of those to find their referenced assemblies and so on...

Hope we all helped !


Wednesday, December 23, 2009 8:09 AM

As i didn't got answer for the post i was scanning the folder with my req dll's and getting list.. Just i had to ensure that copy local is set as true..

Vincent, in your msg you hv written that we can read the manifest, do u hv any code or any idea on how exactly this will be done ?IMP : There might be a delay in posting replies due to time difference (GMT +2:00) and Friday's Holiday instead of Sunday.