简体   繁体   English

基于解决方案的条件程序集引用

[英]conditional assembly references based on solution

I have a rather large solution consisting of about 10 different projects. 我有一个相当大的解决方案,包含大约10个不同的项目。 Until now we have shipped the entire solution as a whole to customers, but we are looking into shipping a stripped version of our software. 到目前为止,我们已经将整个解决方案作为整体交付给客户,但是我们正在考虑交付软件的简化版本。

To this end I would like to exclude several projects from the solution. 为此,我想从解决方案中排除几个项目。 I know that you can prevent projects from being built in the solution configuration manager. 我知道您可以防止在解决方案配置管理器中构建项目。 Using macros all code references can be disabled when the stripped configuration is chosen. 当选择剥离配置时,可以使用宏禁用所有代码引用。 Unfortunately this does not take care of the project references. 不幸的是,这并不涉及项目参考。 Can I make these references conditional depending on the chosen configuration? 我可以根据所选择的配置使这些引用有条件吗?

It should be a pretty simple matter to remove the project references from the project file using a small script - it would just be a case of removing lines adding those references. 使用小脚本从项目文件中删除项目引用应该很简单-这只是删除添加这些引用的行的一种情况。 The project file format is quite simple. 项目文件格式非常简单。

I suspect that's likely to be the easiest solution. 我怀疑这可能是最简单的解决方案。

There's lots of info in http://bytes.com/topic/net/answers/444853-conditional-assembly-reference that I found very useful for resolving a similar question (it pertains more to assembly references than project references though). http://bytes.com/topic/net/answers/444853-conditional-assembly-reference中有很多信息,我发现它对于解决类似问题非常有用(尽管它比项目参考更重要于装配参考)。 MSBuild get assembly reference from ProjectReference might help with bridging between. MSBuild从ProjectReference获取程序集参考可能有助于在两者之间架桥。

Your best bet is to create separate projects for your "stripped down" solution that references only those other projects you want. 最好的选择是为“精简”的解决方案创建单独的项目,该项目仅引用您想要的其他项目。 Reference the same code. 引用相同的代码。 Create a separate solution to hold those projects together. 创建一个单独的解决方案以将这些项目结合在一起。

They can all live together in the same folder structure, too. 它们也可以一起生活在同一文件夹结构中。

For example: 例如:

  • MySolution/MySolution.sln MySolution / MySolution.sln
  • MySolution/MyStrippedDownSolution.sln MySolution / MyStrippedDownSolution.sln
  • MySolution/MyProject1/MyProject1.csproj MySolution / MyProject1 / MyProject1.csproj
  • MySolution/MyProject1/MyStrippedDownProject1.csproj MySolution / MyProject1 / MyStrippedDownProject1.csproj
  • MySolution/MyProject1/MyClass1.cs MySolution / MyProject1 / MyClass1.cs
  • MySolution/MyProject2/MyProject2.csproj MySolution / MyProject2 / MyProject2.csproj
  • MySolution/MyProject2/MyStrippedDownProject2.csproj MySolution / MyProject2 / MyStrippedDownProject2.csproj
  • MySolution/MyProject2/MyClass2.cs MySolution / MyProject2 / MyClass2.cs
  • MySolution/MyProject2/MyProject3.csproj MySolution / MyProject2 / MyProject3.csproj
  • MySolution/MyProject2/MyClass3.cs MySolution / MyProject2 / MyClass3.cs

    • MyProject1 and MyStrippedDownProject1 reference MyClass1 MyProject1和MyStrippedDownProject1参考MyClass1
    • MyProject2 and MyStrippedDownProject2 reference MyClass2 MyProject2和MyStrippedDownProject2参考MyClass2
    • MyProject3 and MyStrippedDownProject3 reference MyClass3 MyProject3和MyStrippedDownProject3参考MyClass3
  • MySolution references MyProject1 and MyProject2 and MyProject3 MySolution引用MyProject1和MyProject2和MyProject3

  • MyStrippedDownSolution references MyStrippedDownProject1 and MyStrippedDownProject2 MyStrippedDownSolution引用MyStrippedDownProject1和MyStrippedDownProject2
  • MyProject1 references MyProject2 and MyProject3 MyProject1引用MyProject2和MyProject3
  • MyStrippedDownProject1 only references MyStrippedDownProject2 -- it does not reference - MyProject3 MyStrippedDownProject1仅引用MyStrippedDownProject2-不引用-MyProject3

I think that this should be done when you build your projects for production. 我认为应该在构建用于生产的项目时完成。 Simply enclose all code that needs to disappear between a compiler-level IF, that checks for a defined compiler directive. 只需将需要消失的所有代码放在编译器级IF之间,即可检查已定义的编译器指令。 If on, then the code between the IF and ENDIF will not compile. 如果启用,则IF和ENDIF之间的代码将无法编译。 This works for code files only. 这仅适用于代码文件。

For anything other that you need to take out, just make up your own way, depending on the file format, as Jon Skeet mentioned. 正如Jon Skeet所述,对于其他需要删除的内容,只需根据文件格式自行调整即可。 But once you put that into build scripts, you need not care about it any more - no switching configurations, etc. 但是,一旦将其放入构建脚本中,就不必再在乎它了-无需切换配置,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM