4

On "normal" .NET assemblies targeting .NET Framework 4, I can use AppDomain.CurrentDomain.GetAssemblies() to get a list of all loaded assemblies.

How to do this on a Windows Universal App or on a CoreCLR application?

3 Answers 3

5

One way to kinda do what you want is to get the DLLs/assemblies which is located in the folder in which your app is installed (which one can assume in some cases is being used/loaded in your app).

    public static async Task<List<Assembly>> GetAssemblyList()
    {
        List<Assembly> assemblies = new List<Assembly>();

        var files = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();
        if (files == null)
            return assemblies;

        foreach (var file in files.Where(file => file.FileType == ".dll" || file.FileType == ".exe"))
        {
            try
            {
                assemblies.Add(Assembly.Load(new AssemblyName(file.DisplayName)));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

        }

        return assemblies;
     }

Then to loop through them you could do:

foreach (var assembly in GetAssemblyList().Result)
{
   //Do something with it
}

Credits to this reddit thread/user.

2
  • Compare the output of this in Debug vs Release to see if you're getting all the info you're expecting.
    – jchristof
    Oct 3, 2017 at 13:09
  • FYI, you might not find all the dlls your looking for. It depends on how "smart" the .NET Native Compiler decides to be. If you don't have any direct code references, it'll toss the dll (we're using MEF).
    – James Esh
    Dec 20, 2018 at 20:50
4

How to do this on a Windows Universal App or on a CoreCLR application?

No, you can’t. Comparing with the Full.NET, the .NET for UWP only provides limited-level reflection functions. In .NET for UWP, you can only get the assembly information by using the code below.

var assembly = this.GetType().GetTypeInfo().Assembly;
1
  • Is there any resource we can use that definitively says which APIs are available for UWP? Even when starting on a reference to the PCL or UWP reflection API, as soon as I go to click on the rest of the namespace, I'm taken to the normal .NET classes. It's frustrating, to say the least, running against roadblocks without any warning whatsoever. Nov 19, 2016 at 19:49
0

In our case, we can use AppDomain.CurrentDomain.GetAssemblies() which is in .NET Standard.

The only problem with this is that it won't find the assembly if the there are no direct code references to the project, even though there is a project reference. This was the case for us, as we use MEF to do things. To work around this, we just set up a dummy code reference.

I love "smart" compilers :)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.