如何在运行时读取给定项目中的MSBuild属性? [英] How to read a MSBuild property in a given project in runtime?

查看:83
本文介绍了如何在运行时读取给定项目中的MSBuild属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问单元测试(一个.NET 4.5类库项目(经典csproj))中的MSBuild变量,但是我找不到任何讨论如何将值从MSBuild传递到执行上下文的文章. /p>

我考虑过在编译过程中设置环境变量,然后在执行过程中读取该环境变量,但这似乎需要一个自定义任务来设置环境变量值,我对此有点担心(理想情况下,我只希望它对当前正在执行的项目可用,而不是全局可用.)

是否存在一种已知的解决方案,可以在运行时从DLL项目内部读取MSBuild属性?可以在执行过程中以某种方式将MSBuild属性作为参数传递"吗?

解决方案

我最终通过使用.Net Core项目中默认使用的相同代码生成任务来使其工作.唯一的区别是我必须在csproj文件中手动添加Target才能使其正常工作,因为代码创建对于框架项目不是标准的:

<Target Name="BeforeBuild">
  <ItemGroup>
    <AssemblyAttributes Include="MyProject.SolutionFileAttribute">
      <_Parameter1>$(SolutionPath)</_Parameter1>
    </AssemblyAttributes>
  </ItemGroup>
  <WriteCodeFragment AssemblyAttributes="@(AssemblyAttributes)" Language="C#" OutputDirectory="$(IntermediateOutputPath)" OutputFile="SolutionInfo.cs">
    <Output TaskParameter="OutputFile" ItemName="Compile" />
    <Output TaskParameter="OutputFile" ItemName="FileWrites" />
  </WriteCodeFragment>
</Target>

使用CompileFileWrites的行可以很好地播放干净等(请参见上面我的评论中的链接答案).其他所有内容都应该足够直观.

项目编译时,会向程序集中添加一个自定义属性,然后我可以使用常规反射进行检索:

Assembly
    .GetExecutingAssembly()
    .GetCustomAttribute<SolutionFileAttribute>()
    .SolutionFile

这确实很好,并且可以避免对解决方案文件进行任何硬编码搜索.

I want to access a MSBuild variable inside an unit test, which is a .NET 4.5 class library project (classic csproj), but I failed to find any articles discussing a way to pass values from MSBuild into the execution context.

I thought about setting an environment variable during compilation and then reading that environment variable during execution, but that seems to require a custom task to set the environment variable value and I was a bit worried about the scope of the variable (ideally, I only wanted it to be available to the currently executing project, not globally).

Is there a known solution to reading an MSBuild property from inside a DLL project in runtime? Can MSBuild properties be "passed as parameters" during execution somehow?

解决方案

I finally made it work by using the same code generation task that is used by default in .Net Core projects. The only difference is that I had to manually add the Target in the csproj file for it to work, as code creation is not standard for framework projects:

<Target Name="BeforeBuild">
  <ItemGroup>
    <AssemblyAttributes Include="MyProject.SolutionFileAttribute">
      <_Parameter1>$(SolutionPath)</_Parameter1>
    </AssemblyAttributes>
  </ItemGroup>
  <WriteCodeFragment AssemblyAttributes="@(AssemblyAttributes)" Language="C#" OutputDirectory="$(IntermediateOutputPath)" OutputFile="SolutionInfo.cs">
    <Output TaskParameter="OutputFile" ItemName="Compile" />
    <Output TaskParameter="OutputFile" ItemName="FileWrites" />
  </WriteCodeFragment>
</Target>

The lines with Compile and FileWrites are there for it to play nicely with clean and such (see linked answers in my comments above). Everything else should be intuitive enough.

When the project compiles, a custom attribute is added to the assembly, that I can then retrieve using normal reflection:

Assembly
    .GetExecutingAssembly()
    .GetCustomAttribute<SolutionFileAttribute>()
    .SolutionFile

This works really well and allows me to avoid any hardcoded searches for the solution file.

这篇关于如何在运行时读取给定项目中的MSBuild属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆