MSBuild从ProjectReference获取程序集参考

编程入门 行业动态 更新时间:2024-10-22 13:30:06
本文介绍了MSBuild从ProjectReference获取程序集参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个MSBuild任务,该任务将一些代码生成添加到标准C#项目(.csproj)中.该任务需要访问该项目引用的程序集中的类型.

I am writing a MSBuild task that adds some code generation to a standard C# project (.csproj). The task needs to access types in assemblies referenced by that project.

这对于程序集引用很简单(在< Reference>中获取所有项),但是对其他项目的引用(< ProjectReference>)则变得更加困难

This is simple for assembly references (get all items in <Reference>), but it gets harder for references to other projects (<ProjectReference>)

MSBuild是否提供一种从< ProjectReference>中检索编译的程序集引用的方法?

Does MSBuild provide a way to retrieve a compiled assembly reference from a <ProjectReference>?

如果没有,是否有一种简单的方法可以通过读取.csproj文件来解析该名称?

If not, is there a simple way to resolve that name by reading the .csproj file?

.csproj文件不直接提供已编译的程序集路径,必须从其他属性中重建它.另外,某些属性是有条件的(取决于Debug/Release配置),因此使用简单的XPath阅读器将行不通:

The .csproj file does not directly provide the compiled assembly path, it has to be reconstructed from other properties. Also, some of the properties are conditional (depending on Debug/Release configuration), so using a simple XPath reader wouldn't work:

Dll文件名可以从< AssemblyName>获得,但是Dll文件的写入路径位于

Dll file name can be obtained from <AssemblyName>, but the path where the Dll file is written is in

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <OutputPath>;bin\Release</OutputPath> <PropertyGroup>

是否可以通过编程方式读取.csproj文件并通过评估所有条件来解析OutputPath的正确值?

Is there a way to programmatically read the .csproj file and resolve the correct value of OutputPath by evaluating all conditions?

我需要一个解决方案,其中引用的.csproj文件保留为普通的旧项目文件(不对csproj文件进行任何修改,而这些修改将以更易于访问的方式添加必要的信息)

I need a solution where the referenced .csproj files remain plain old project files (no modifications to the csproj files that would add the necessary information in a more accessible way)

推荐答案

您可以输入以下内容:

<Target Name="CopyDllsFromDependentProjects"> <MSBuild Projects="@(ProjectReference)" Targets="Build" BuildInParallel="true" Condition="'%(Name)'=='ProjectA' Or '%(Name)'=='ProjectB'"> <Output TaskParameter="TargetOutputs" ItemName="OutputAssemblies" /> </MSBuild> <Copy SourceFiles="@(OutputAssemblies)" DestinationFolder="$(PostBuildDir)" SkipUnchangedFiles="true" />

进入您的项目并按以下方式调用它:

in to your project and call it like this:

<Target Name="AfterCompile" DependsOnTargets="CopyDllsFromDependentProjects" />

添加调味料.

这涉及MSBuild依赖关系以计算出输出(由于MSBuild处理内含物的方式而无法静态导出信息-例如,TeamBuild将输出放在何处?).

This involves MSBuilding the dependencies to work out the outputs (the info cant statically be derived due to the way in which inclusions are processed in MSBuild - e.g., where would TeamBuild put the outputs?).

《 MSBuild Engine内幕》一书非常适合深入探讨这种胡说八道.

The Inside the MSBuild Engine book is great for digging into this sort of nonsense.

更多推荐

MSBuild从ProjectReference获取程序集参考

本文发布于:2023-11-13 14:33:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   MSBuild   ProjectReference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!