Get list of loaded Assemblies on UAP10 platform

和自甴很熟 提交于 2019-11-28 11:37:27

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.

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;

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!