Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Assemblies Without Instantiating Them

I am trying to get all assemblies in CurrentDomain using AppDomain.CurrentDomain.GetAssemblies() to write their FullName into a DropDownList, however if I don't instantiate them, they are not seen in returned array from GetAssemblies() in CurrentDomain.

They are all added as Reference and in Reference folder of the solution. Only time I can get them from the GetAssemblies() is when I first instantiate them.

How to overcome this problem with an easy and more generic way instead of instantiate them everytime when I add new Assembly, etc.

Due to company policy, I have to obfuscate some parts of the images:

enter image description here

All the assembilies are referenced in Reference folder:

enter image description here

like image 966
Tarik Avatar asked Jan 17 '23 01:01

Tarik


1 Answers

There is actually something subtle that happens here in addition to the other people's comments.

VisualStudio actually does not add .dll references to the assembly manifest of your built assembly's reflection info unless they are actually used.

As a sample, make a new project, and reference an assembly. I used nunit.framework.dll as an example.

But don't actually use it. My code is simply:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

But the Project does have a reference to NUnit in VisualStudio. Now build it, and open the assembly in ildasm.exe and the top of the manifest is:

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly ConsoleApplication1
{
    ...etc...

Now, in the code, just use anything from NUnit:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Assert.IsTrue(true);
        }
    }
}

Again rebuild and check the assembly manifest in ildasm.exe:

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern nunit.framework
{
  .publickeytoken = (96 D0 9A 1E B7 F4 4A 77 )                         // ......Jw
  .ver 2:6:0:12051
}
.assembly ConsoleApplication1
{
    ...etc...

Note that now there is an extern in the IL. This also feeds reflection, so it knows what the dependent assemblies are to be loaded.

Past that little detail, as other have stated, the runtime doesn't actually load an assembly until it is needed for the first time, hence your AppDomain doesn't have the assemblies loaded until you instantiate or use something from that assembly.


That detail above comes into play when you start to try to use the Assembly.GetReferencedAssemblies() method.

So in VisualStudio, I have a project that has these references:

Microsoft.CSharp
nunit.framework
System
System.Core
System.Data
System.Xml
System.Xml.Linq

Then I have the C# code:

using System;
using System.Reflection;
using NUnit.Framework;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
            foreach (var assemblyName in assemblies)
            {
                Console.WriteLine(assemblyName.FullName);
            }
            Console.ReadKey();
        }
    }
}

Note that I even have a using NUnit.Framework; statement! But I don't actually use NUnit anywhere, so it is not a referenced assembly. The output of running this is:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

That is it. If I add this line into my test app:

Assert.IsTrue(true);

Now something actually uses NUnit,a nd my console output is:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
nunit.framework, Version=2.6.0.12051, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77
like image 193
CodingWithSpike Avatar answered Jan 27 '23 06:01

CodingWithSpike