简体   繁体   中英

Get Types from assembly loaded by UWP app

I am wondering what is the best way to get the types defined in assemblies referenced by a UWP application (.Net Native compiled).

I am using an IOC container and like to register the types based on a convention.

Assembly.GetAssembly(typeof(MainPage))
    .DefinedTypes
    .Where(t => t.IsClass && t.IsPublic)
    .ToList()

The above code only gives me the types defined in the UWP applications and not the referenced assemblies.

UPDATE

I am able to get the types from the referenced assembly if I know the name

var types = Assembly.Load(new AssemblyName("Simple")).ExportedTypes.ToList();

However, I am not able to discover the loaded assemblies. The statement below throws an exception.

var names = Assembly.GetAssembly(typeof(MainPage)).GetReferencedAssemblies();

在此处输入图片说明

Following is a way to get all the referenced assemblies. Hope it will be useful to you. You can further loop the assemblies to get the Type OR other required stuff. It will fetch all the DLL and EXE files from the location where your app is installed.

You can check this is with the Debug and Release both modes for the difference.

public static async Task<List<Assembly>> GetReferecedAssemblies()
{
    List<Assembly> refAssemblies = new List<Assembly>();

    var allFiles = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();
    var filteredFiles = allFiles.Where(file => file.FileType == ".dll" || file.FileType == ".exe");

    if (filteredFiles != null && filteredFiles.Count() > 0)
    {
        foreach (var file in filteredFiles)
        {
            try
            {
                refAssemblies.Add(Assembly.Load(new AssemblyName(file.DisplayName)));
            }
            catch (Exception ex)
            {
            }
        }
    }

    return refAssemblies;
}

OUTPUT from Immediate Window

Count = 103
    [0]: {App1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
    [1]: {Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [2]: {Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [3]: {Microsoft.Win32.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [4]: {System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [5]: {System.Collections.Concurrent, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [6]: {System.Collections, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [7]: {System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [8]: {System.Collections.NonGeneric, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    [9]: {System.Collections.Specialized, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}
    // more here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM