我可以将编译常量传递给项目参考吗?

Jas*_*ock 7 c# msbuild

如果我有一个<ProjectReference>引用,有没有办法将条件编译值传递给该项目?像这样的东西(我知道<DefineConstants>这样不存在,只是为了说明需要):

<ProjectReference Include="..\DataProducer\DataProducer.csproj">
  <DefineConstants>WAS_SET</DefineConstants>
<ProjectReference>
Run Code Online (Sandbox Code Playgroud)

因此,如果该项目中有一个类,如下所示:

    public sealed class ProduceValue
    {
        public string Produce()
        {
#if WAS_SET
            return "WAS SET";
#else
            return "NOTHING WAS SET";
#endif
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后通过在编译期间传递该值,我可以获得不同的输出.

Mar*_*ich 6

ProjectReference项目允许添加元数据字段PropertiesUndefineProperties允许操作用于构建项目引用的“全局”属性集。您可以通过将全局属性传递给引用的项目来使用它,如下所示:

<ProjectReference Include="..\DataProducer\DataProducer.csproj">
  <Properties>DefineConstants=WAS_SET</Properties>
<ProjectReference>
Run Code Online (Sandbox Code Playgroud)

现在作为引用项目的全局属性的效果是它将覆盖DefineConstants项目中静态定义的任何定义/更新- 这也可能包括任何添加的配置常量 ( DEBUG)。