如何在多个Visual Studio项目之间共享属性? (尤其是 C# 项目)

发布于 2024-09-18 21:49:27 字数 1051 浏览 4 评论 0原文

手动保持多个 Visual Studio 项目的属性同步很烦人。那么,如何在多个项目之间共享属性呢?

编辑:我指的是条件编译符号、警告和错误处理等属性,即您可以在“项目”->“属性”选项卡中或通过编辑项目 XML 文件来配置的内容。

类似的问题有之前被问过,请参阅:123。然而,根据我的理解,答案是特定于 C++ 的。我正在寻找 C# 项目的答案。尽管如此,如果您保持清晰的分离,请毫不犹豫地回答其他类型的项目(Visual Basic 等),因为除了我之外还有其他人可能感兴趣。

这篇博文 提出了问题的解决方案,但我更喜欢更简单的方法。

另外,您至少可以通过以下方式解决部分问题(注意,虽然我测试了它,但我没有彻底测试它):

使用您打算共享的程序集属性创建一个 AssemblyInfo.cs 文件。链接到各个项目中的现有项目。使用原始(本地)AssemblyInfo.cs 并将项目特定的程序集属性放在那里。不幸的是,覆盖属性似乎不起作用,并且通过 GUI 管理属性现在受到限制。

Keeping properties of multiple Visual Studio projects manually in sync is annoying. So, how can you share properties between multiple projects?

Edit: I refer to properties like conditional compilation symbols, treatment of warnings and errors etc., i.e., things you can configure in Project->Properties tabs or by editing the project XML file.

Similar questions have been asked before, see: 1, 2 and 3. However, in my understanding, the answers have been C++-specific. I am looking for an answer for C# projects. Nevertheless, do not hesitate to answer for other kinds of projects (Visual Basic etc.) if you keep the separation clear, because someone else than me might be interested.

This blog post proposes a solution to the problem, but I would prefer something simpler.

Also, you can at least solve a part of the problem in the following way (note that although I tested it, I did not test it thoroughly):

Create an AssemblyInfo.cs file with the assembly attributes you intend to share. Link to this existing item in the individual projects. Use the original (local) AssemblyInfo.cs and put project-specific assembly attributes there. Unfortunately, overriding attributes does not seem to work, and managing the attributes via the GUI is now limited.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-09-25 21:49:27

对于这类事情,我更喜欢有一个单独的类库项目,其中一个(或多个)静态类存储(静态)属性。然后,从需要同步这些属性的每个项目中添加对该项目的引用,所有这些项目都将具有相同的值,您只需在一处更改它。

例如,假设我有相同的 Web 和桌面形式的应用程序。两者的连接字符串等内容必须相同。所以我将创建三个项目:
MyProject.Web(Web 应用程序)
MyProject.Desktop(Windows 窗体应用程序)
MyProject.Common(类库)
然后,我在 Common 中添加一个名为 Properties 的新静态类,其中包含一个名为 ConnectionString 的静态属性,该属性返回连接字符串。
然后,我在 Web 和桌面中添加对 Common 的引用,当我想从其中任何一个访问连接字符串时,我使用 Common.Properties.ConnectionString。

For that kind of things, I prefer to have a separate Class Library Project, with one (or more) static classes storing the (static) properties. Then add a reference to that project from every project that needs to have those properties in sync, and all those projects will have the same values and you have to change it in only one place.

For example, let's say that I have the same app in web and desktop form. Things like connection strings and such will have to be the same for both. So I will create three projects:
MyProject.Web (Web application)
MyProject.Desktop (Windows forms application)
MyProject.Common (Class library)
Then I add a new static class in Common called Properties with a static property called ConnectionString that returns the connection string.
I then add a reference to Common in Web and Desktop, and when I want to access the connection string from any of them I use Common.Properties.ConnectionString.

八巷 2024-09-25 21:49:27

我们大量使用 .vsprops 文件来在我们的本机项目之间定义共享宏。

有人提出了与您完全相同的问题,并提出了向解决方案添加“空白”视觉 C++ 项目的想法,以便可以导入 vsprops 文件,并且属性通常对解决方案的其余部分可见。如果这听起来不太恶心,我可以看看它是如何实现的。

We make very heavy use of the .vsprops files to have shared macros defined between our native projects.

Someone asking exactly the same question as you came up with the idea of adding a "blank" visual C++ project to the solution so that could import the vsprops file and the properties would be generally visible to the rest of the solution. If it doesn't sound too gross a hack, I can find out how it worked out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文