Get list of loaded Assemblies on UAP10 platform

随声附和 提交于 2019-11-27 06:18:29

问题


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?


回答1:


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:


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;



回答3:


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



来源:https://stackoverflow.com/questions/32487141/get-list-of-loaded-assemblies-on-uap10-platform

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