7

I would like two how to do to share Resources files between 2 (or more) projects?

So, to resume, I've three project :

the development project (CF.NET) that include the resource file (with all definition).

I've two other projects that are empty BUT linking to the development projects, it's just a different build each time, so when I modify the development project, all three projects are updated too. (Modification of the csproj file.)

Question is, what about Resources files? When I try to access from the development project I get all resources but when I try from the 2 others, it throws an "MissingManifestResourceException".

Any idea how to solve this issue?

Thanks.


[EDIT]

Here is what I've done :

Create a project named "RealProject" which contains all code (including resources files) Create a project named "LinkedProject" which contains nothing (I deleted all files into it and modify the csproj file as the following :

  <ItemGroup>
    <Compile Include="..\RealProject\**\*.cs" />
  </ItemGroup>

So in LinkedProject directory I've only :

  • [Directory] bin
  • [Directory] obj
  • [File ] LinkedProject.csproj

The whole LinkedProject uses the RealProject files, it's just a different configuration build (see here to know why : C# - Code compiler for .NET & CF.NET )

Once in that configuration, I've no access to the resources files from the RealProject ...

If you need screens or more detailed explanation, just ask.


[EDIT]

With this code, it works, Resource manager isn't loaded on the good Assembly name, but it should exists a better solution !!!

Assembly ass = Assembly.ReflectionOnlyLoadFrom(@"..\..\..\RealProject\bin\Debug\RealProject.dll");
ResourceManager manager = new ResourceManager("RealProject.Properties.Resources", ass);

[Solution]

Things to check :

  • The LinkedProject as the same namespace as the RealProject
  • Add Resources as links
  • Clean up all your solution
  • Rebuild it

Test !

Community
  • 1
  • 1
Arnaud F.
  • 8,252
  • 11
  • 53
  • 102

3 Answers3

7

Try to add the resource file as a link to the other two projects and make sure the namespaces as defined in the project file is the same.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

Try adding existing file in other projects as a link.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

The problem with sharing resources files between different projects is that the root namespace has to be the same in all the projects you use the same file in.

Or not.

You can get the root namespace at runtime in the *Resources.designer.cs file. Note the links in the comments to related answers. Make sure you commit this and keep an eye on it, the code-generator has a habit of overwriting it which would break its universality. I used the xml doc comments to remind me what's going on, if the code-gen obliterates it I'll see it in the commit diff.

/// <summary> Returns the cached ResourceManager instance used by this class.  </summary>
/// <remarks> DO NOT commit any version of this file the tool overwrites, it will break it for other projects</remarks>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Resources.ResourceManager ResourceManager {
    get {
        if (object.ReferenceEquals(resourceMan, null)) 
        {
            // https://stackoverflow.com/a/1329631/492 https://stackoverflow.com/a/51978052/492  
            Assembly thisAssembly = typeof(/*thisClassName*/).Assembly;
            // you need a class called "App", or just change the name on the next line to one you have in the root namespace
            var apps = thisAssembly?.GetTypes().Where(t => t.Name == "App");

            if (apps.Count() != 1)
                throw new InvalidOperationException($"Too Many App classes. Count: {apps.Count()}, Apps: {apps}");
            
            Type appType = apps.FirstOrDefault();
            string ns = appType.Namespace;
            global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager($"{ns}.OtherNames.Spaces.thisClassName", typeof(thisClassName).Assembly);
            resourceMan = temp;
        }
        return resourceMan;
    }
}
CAD bloke
  • 8,578
  • 7
  • 65
  • 114