Access resource within the calling assembly - c#

I'm moving some code from an application to a helper class project. This application uses resource files that hold texts and their translations.
Is there a way to still access the resource files from the application within that new helper project? This would be very helpful, because then i don't have to move half of those resource files to that new project.
Assembly A (contains the resource file) -> Calls method on Assembly B and this method needs a resource text from Assembly A.

You could use Assembly.GetEntryAssembly().GetFile() method in dll to get the file from application assembly and use ResourceManager class to load it. Or possibly use Assembly.GetEntryAssembly().GetExportedTypes() to get strongly typed resource class. However, you should consider if referencing resources in application from library is the right approach. I would suggest either moving all resources to library assembly and referencing them from application, or (probably the best) - create separate assembly only for resources and use it both from app and dll.

Related

Is there a way to reload a localized dll on the fly?

I have a project (WPF) that is using a localization assembly that contains all the resource strings.
This localization assembly is given to a translation service who creates a new localized satellite version of this assembly.
The main application is then supposed to be able to import the newly localized dll. That works, but I can't figure out a way to have the new language available without restarting the application.
Is that possible ?

Assembly or file not found on plugin system C#

I have a project that uses plugins based on an interface, they are coded in other "Class Library" projects in the same main project solution. The main project has a folder that contains all the plugins in dll format. So for example I have a Plugins folder in "Bin/Debug/". All the plugins are compiled on that directory.
Each plugin project has a reference to the main project as it uses the main framework (reference not copied locally).
Each plugin should be able to use methods from other plugins, here I have the problem.
Example:
I have PluginA and PluginB, 2 DLLs, 2 different projects but the DLLs are in the same Plugins folder.
Now I want to instantiate PluginB in PluginA class so I add a reference to PluginB DLL (not copied locally). No errors from the Compiler.
But when I istantiate PluginA on the main project after loading all the plugins assemblies, I get a System.IO.FileNotFoundException Assembly or file not found. (referred to PluginB)
Basically seems that when I call a Plugin, from the main project, that calls another Plugin, I get an error. I've also tried to use the Assembly Resolve event without success.
Maybe I made some mistakes with the entire plugins system, it's the first time I use them.
Sorry for my english, I'm italian.
Thanks for the help.

WPF Dynamically load resource dictionaries

I have a WPF assembly, besides exe file, a have a folder "Component", where the user can put custom DLL with components. These DLL files consist of ViewModels and Resource Dictionaries(where data templates for viewmodels are described).
These DLL assemblies are loaded by reflection.
Question: how I can dynamically load all dictionaries from these assemblies to my App.xaml?
When you load these assemblies I'm guessing you also load a main component, something crucial for it to work, like a module.
You can have all you modules implement an interface with a GetResourceDictionaries method that you call from the application when intintializing.
Then have all gathered ResourceDictionaries added to your Application.Resources.

loading embedded resources which live in a different assembly

I have a Visual Studio 2015 solution which consists of an executable and a series of DLL projects. All code is managed C# and running on Windows 10 64-bit.
One of the DLLs needs to load a resource specified by the application. The app provides the full manifest resource path and the DLL uses this to load it.
The resources are set as "Embedded Resource" in solution explorer. An example path would be something like "The.DLL.Assembly.subdir.resources.image.png".
The DLL uses the below code to load the resource.
var asm = Assembly.GetExecutingAssembly();
var resStream = asm.GetManifestResourceStream(path);
This works great! Well, that is if the resource in question is stored inside the DLL assembly which is trying to load it. We'd prefer, though, not to have to locate all the resources in this DLL assembly. We'd like the freedom to let the application locate them in any of the projects and just provide the path letting the loading DLL know how to find them. This would allow the app to better organize the resources based on which project "owns" them as opposed to being forced to put them all in the assembly which will actually load them.
Where I'm stuck is being able to load the resource if it lives in another assembly.
I've tried:
GetEntryAssembly(), this returns the executable assembly, but loading the resource fails even with the full path, it appears GetManifestResourceStream() will only look in that assembly
GetEntryAssembly().GetModules(), thinking I could somehow find the other assembly this way, but this returns a single entry, just the exe itself
Any ideas? I'm thinking there must be a way to load an embedded resource which lives in another assembly, but I haven't yet found it.

.Net shared class library configuration

I have a class library which needs some configuration to function. This class library is referenced by multiple applications (Multiple ASP.Net websites, and Windows Forms applications)
It is my understanding that it is possible to store the configuration in the library's app.config => myDll.dll.config file. See: Putting configuration information in a DLL, and C# Dll config file
My issue is that I don't want to manually handle copying the config file to the bin folder of every host assembly. Is there a mechanism in .Net to handle pairing of the dll to its config file so that the accompanying configuration is copiled along with the dll whereever it is distributed/referenced?
If the config is the same for all instances of your dll, then I'd add it as an embedded resource, so it's part of your dll and not a separate file at all.
TO do this, either add it as a file resource to your Resources.resx file, or just add the file directly to your Project and then set its compile type (in the Properties window) to Embedded Resource.
You can then use Assembly.GetExecutingAssembly.GetManifestResourceNames() to list the names of the resources in your dll, and Assembly.GetExecutingAssembly.GetManifestResourceStream() to get a stream to read the file's data from. I'd probably use a simple homebrew XML format for my data and then an XmlTextReader/XmlDocument to read it very easily back in.
You'l have to deploy this .dll into GAC and put there the config file, all apps will first search the GAC when loanding a reference. Here is how you can deploy the dll + config.

Categories