Search code examples
c#reflectionwindows-8windows-store-apps.net-assembly

Get Type from Fully qualified name only in Windows Store App


I have a string containing a full qualified name like MyNamespace.MyType which I know is in a loaded assembly.

I need to get the Type instance of this, in a windows store app.

The issue I'm having is that while there is some reflection classes in windows store apps, it's very limited, and I simply can't use what I've been using for a desktop app.

If I can get an assembly I can find my type within it so I'm currently trying to get all loaded assemblies, which I can't do easily as AppDomain doesn't exist.

I found the following:

private async System.Threading.Tasks.Task<IEnumerable<Assembly>> GetAssembliesCore()
{
    var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

    List<Assembly> assemblies = new List<Assembly>();
    foreach (Windows.Storage.StorageFile file in
                 await folder.GetFilesAsync().AsTask().ConfigureAwait(false))
    {
        if (file.FileType == ".dll")
        {
            AssemblyName name = new AssemblyName() { Name = file.Name };
            Assembly asm = Assembly.Load(name);
            assemblies.Add(asm);
        }
    }

    return assemblies;
}

I added the .AsTask().ConfigureAwait(false) to stop it hanging, but it now fails trying to load the assembly:

"Could not load file or assembly 'FDE.Audio.dll' or one of its dependencies.
The system cannot find the file specified.":"FDE.Audio.dll"

Is there something I need to set up in the manifest? Something else?

How can I load an assembly in my program's folder (AppX I think)?


Solution

  • Try to extract file name without extension

    var filename = file.Name.Substring(0, file.Name.Length - file.FileType.Length);
    
    AssemblyName name = new AssemblyName() { Name = filename };
    Assembly asm = Assembly.Load(name);