Accessing a string resource from a class library

I would like to know how to load a string resource from another class library. Here is my structure.

Solution\ CommonLibrary\ EmbeddedResource.resx MainGUI\ 

If I get a string in the CommonLibrary classes, I just use EmbeddedResource.INFO_START_MSG, but when I try to use a typed string resource, it cannot recognize the resource file. Note that CommonLibrary is already mentioned in MainGUI.

I usually do that.

 Solution\ CommonLibrary\ MainGUI\ EmbeddedResource.resx 

But I want to use the same resource for both projects.

+4
source share
3 answers

Add the library link to the main application. Verify that (in the Resources file) the Access Modifier is set to public.

Link to the line as follows:

 textBox1.Text = ClassLibrary1.Resource1.ClassLibrary1TestString; 

I added the resource file with a right-click, thus "1" in the name. If you go to the "Properties" page for the class library and go to the "Resources" tab, you can add a default resource file that will not have the number "1" in the name.

Just make sure your values ​​are publicly available, and that you have a link in the main project, and you shouldn't have a problem.

+11
source

By default, the resource class is internal , which means that it will not be directly accessible in other assemblies. Try changing it to public . Part of this you will also need to make string properties in the public resource class

+3
source

I have done this in the past. However, this may not work through assemblies:

 public static Stream GetStream(string resourceName, Assembly containingAssembly) { string fullResourceName = containingAssembly.GetName().Name + "." + resourceName; Stream result = containingAssembly.GetManifestResourceStream(fullResourceName); if (result == null) { // throw not found exception } return result; } public static string GetString(string resourceName, Assembly containingAssembly) { string result = String.Empty; Stream sourceStream = GetStream(resourceName, containingAssembly); if (sourceStream != null) { using (StreamReader streamReader = new StreamReader(sourceStream)) { result = streamReader.ReadToEnd(); } } if (resourceName != null) { return result; } } 
+2
source

Source: https://habr.com/ru/post/1396176/


All Articles