C# 从解决方案中的多个项目引用资源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:46:33  来源:igfitidea点击:

Referencing resource files from multiple projects in a solution

提问by Joda

I am working on localization for a asp.net application that consists of several projects.

我正在为由多个项目组成的 asp.net 应用程序进行本地化。

For this, there are some strings that are used in several of these projects. Naturally, I would prefer to have only one copy of the resource file in each project.

为此,在其中几个项目中使用了一些字符串。自然,我更愿意在每个项目中只有一份资源文件副本。

Since the resource files don't have an namespace (at least as far as I can tell), they can't be accessed like regular classes.

由于资源文件没有命名空间(至少据我所知),因此无法像常规类一样访问它们。

Is there any way to reference resx files in another project, within the same solution?

有没有办法在同一个解决方案中引用另一个项目中的 resx 文件?

采纳答案by Jon Limjap

You can just create a class library project, add a resource file there, and then refer to that assembly for common resources.

您可以只创建一个类库项目,在那里添加一个资源文件,然后引用该程序集以获取公共资源。

回答by Martin

Some useful advice on how to manage a situation like this is available here:

此处提供了一些有关如何管理此类情况的有用建议:

http://www.codeproject.com/KB/dotnet/Localization.aspx

http://www.codeproject.com/KB/dotnet/Localization.aspx

回答by Mark Harris

I have used this solution before to share a assembley info.cs file across all projects in a solution I would presume the same would work fro a resource file.

我之前使用过这个解决方案在一个解决方案中的所有项目中共享一个 assembley info.cs 文件,我认为这同样适用于资源文件。

Create a linked file to each individual project/class library. There will be only one copy and every project will have a reference to the code via a linked file at compile time. Its a very elegant solution to solve shared non public resources without duplicating code.

为每个单独的项目/类库创建一个链接文件。将只有一个副本,并且每个项目在编译时都会通过链接文件引用代码。它是一种非常优雅的解决方案,可以在不复制代码的情况下解决共享的非公共资源。

<Compile Include="path to shared file usually relative">
  <Link>filename for Visual Studio To Dispaly.resx</Link>
</Compile>

add that code to the complile item group of a csproj file then replace the paths with your actual paths to the resx files and you sould be able to open them.

将该代码添加到 csproj 文件的编译项目组中,然后将路径替换为您到 resx 文件的实际路径,您将能够打开它们。

Once you have done this for one project file you should be able to employ the copy & paste the linked file to other projects without having to hack the csproj.

为一个项目文件完成此操作后,您应该能够将链接文件复制并粘贴到其他项目,而无需破解 csproj。