Get list of loaded Assemblies on UAP10 platform
Asked Answered
H

3

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?

Hydrocortisone answered 9/9, 2015 at 18:50 Comment(0)
C
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.

Carlicarlick answered 30/6, 2016 at 10:8 Comment(2)
Compare the output of this in Debug vs Release to see if you're getting all the info you're expecting.Uriisa
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).Troyes
C
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;
Capeskin answered 11/9, 2015 at 8:10 Comment(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.Elmoelmore
T
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 :)

Troyes answered 20/12, 2018 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.