4

I'm currently working on a project where I want to automatically build a showcase of all the styles that are defined in the styling project of another Visual Studio solution.

To do this, I select the Styling.dll from the at runtime via OpenFileDialog from the other solutions bin/Debug folder, add its resources to my applications resources and then build the views. That works pretty well, but now I've got a problem when selecting an assembly with references to several other assemblies.

This is how I load the assemblies right now:

    public void OpenFile()
    {
        var openFileDialog = new OpenFileDialog { Filter = "DLL Dateien (*.dll)|*.dll" };
        if (openFileDialog.ShowDialog() == true)
        {
            var path = Path.GetDirectoryName(openFileDialog.FileName);
            foreach (var dll in Directory.GetFiles(path, "*.dll").Where(dll => dll != openFileDialog.FileName && !dll.ToString().ToLower().Contains("datagrid")))
            {
                this.LoadResources(dll, false);
            }

            this.LoadResources(openFileDialog.FileName);
        }
    }

I put in the foreach to first load a Utils.dll that always comes with the Styling.dll, but that obviously doesnt work if there are multiple other assemblies referencing each other.

Then in LoadResources :

    public void LoadResources(string assemblypath)
    {
        var assembly = Assembly.LoadFile(assemblypath);
        var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".g.resources");
        if (stream != null)
        {
            var resourceReader = new ResourceReader(stream);

            foreach (DictionaryEntry resource in resourceReader)
            {
                // Get all the .baml files from the .dll and create URI for corresponding .xaml files
                if (new FileInfo(resource.Key.ToString()).Extension.Equals(".baml"))
                {
                    var uri = new Uri("/" + assembly.GetName().Name + ";component/" + resource.Key.ToString().Replace(".baml", ".xaml"), UriKind.Relative);

                    // Add ResourceDictionary to Application Resources
                    var rd = Application.LoadComponent(uri) as ResourceDictionary;

                    if (rd != null)
                    {
                        Application.Current.Resources.MergedDictionaries.Add(rd);
                    }
                }
            }
        }
    }

Note: The exception always throws at the var rd = Application.LoadComponent(uri) as ResourceDictionary; point.

Now, how do I get my application to correctly load all the other assemblies in the directory? Right now, I always get an error telling me that the other assemblies could not be found, even though they are in the same directory as the referencing assembly.

After looking for a solution for a while now, I understood that the application automatically tries to find the missing assemblies, but doesn't search in the directory where they actually are. Since i want to load the assemblies dynamically, I also cant just add additional search paths to my app.config beforehand, can I do it at runtime somehow?

I hope you understand my problem, any help is appreciated, thank you!

Edit:

I have already read through this question, but it didnt help me any further. When I implement the AssemblyResolve Event, I still get FileNotFound / XamlParse exceptions, even when the file in the assemblypath exists. Also, the handler always tries to find xxx.resources.dll files, which on the other hand do not exist in the directory.

tobi_fis
  • 55
  • 6
  • 1
    You can use [`AppDomain.ResolveAssembly`](https://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve(v=vs.110).aspx) to help it find assemblies in other directories. [This](https://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net) question has an answer with code you can make use of – Bradley Uffner Jun 21 '17 at 17:37
  • 2
    Possible duplicate of [How to add folder to assembly search path at runtime in .NET?](https://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net) – Bradley Uffner Jun 21 '17 at 17:42
  • 1
    Thanks, I've already seen that topic, but it didnt help me (or I maybe misunderstood it?) When the EventHandler reaches the `File.Exists` point, it never finds the file on the specified path, even though it's there. – tobi_fis Jun 23 '17 at 07:59

0 Answers0