How to load at runtime assemblies that need .resources ?

Horadiluh

New member
Joined
Jun 18, 2021
Messages
2
Programming Experience
5-10
Hi,

I'm writing a plugin based dotnet core GUI app with Avalonia. My plugins are class C# assemblies. I can load them but when thoses plugins wants to use, say Windows Registry manipulation, they want to obviously load Microsoft.Win32.Registry.

I wrote a ResolveEventHandler:
C#:
        static Assembly OnAssemblyResolv(object sender, ResolveEventArgs args)
        {
            try
            {
                string assemblyPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new AssemblyName(args.Name).Name + ".dll");
                Assembly assembly = Assembly.LoadFile(assemblyPath);
                return assembly;
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            return null;
        }

It works for the base library Microsoft.Win32.Registry.dll, but I also get another event on Microsoft.Win32.Registry.resources.

How do I handle that Microsoft.Win32.Registry.resources ? There is no such .dll.
 
It's been a while, but as I recall, the way .NET resource loading works is that it tries to do a few files system probes to determine if some UI resources are being overridden before it finally falls back to resources that actually compiled into the assembly. My recommendation is that if you can find the file, offer the file back. If you can't find the file, then return the appropriate error or return value so that the systems knows that the file doesn't exist.

(It's also long enough for me, but I'm not sure if all the files being searched will necessarily have a ".dll" file extension. I thought that some of them might have different file extensions. You'll have to read up on .NET Framework resource loading to determine if I'm just misremembering.)
 
Hi, thanks for your reply.
I searched for Microsoft.Win32.Registry.resources on my computer and couldn't find it. When referencing Microsoft.Win32.Registry, it works fine, but when loading it through Assembly.LoadFile, it requests that .resources thing.

When returning null (as I should when not finding the Assembly), I got a popup saying: The type initializer for 'Microsoft.Win32.Registry' threw an exception.
 
Back
Top Bottom