生成中未包括MSBuild引用程序集

生成中未包括MSBuild引用程序集,msbuild,csc,Msbuild,Csc,我可以使用以下命令生成我的项目 csc /reference:lib\Newtonsoft.Json.dll SomeSourceFile.cs msbuild MyProject.csproj 。。。但是当我使用这个命令时 csc /reference:lib\Newtonsoft.Json.dll SomeSourceFile.cs msbuild MyProject.csproj 。。。对于以下.csproj文件,不包括my.dll引用。有什么想法吗 <PropertyGro

我可以使用以下命令生成我的项目

csc /reference:lib\Newtonsoft.Json.dll SomeSourceFile.cs
msbuild MyProject.csproj
。。。但是当我使用这个命令时

csc /reference:lib\Newtonsoft.Json.dll SomeSourceFile.cs
msbuild MyProject.csproj
。。。对于以下.csproj文件,不包括my.dll引用。有什么想法吗

<PropertyGroup>
    <AssemblyName>MyAssemblyName</AssemblyName>
    <OutputPath>bin\</OutputPath>
</PropertyGroup>

<ItemGroup>
    <Compile Include="SomeSourceFile.cs" />
</ItemGroup>

<ItemGroup>
    <Reference Include="Newtonsoft.Json">
        <HintPath>lib\Newtonsoft.Json.dll</HintPath>
    </Reference>
</ItemGroup>

<Target Name="Build">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>


MyAssemblyName
垃圾箱\
lib\Newtonsoft.Json.dll

您的参考小组没有连接到Csc任务。此外,无法在任务内直接使用指定方式的引用。MSBuild附带的任务包括ResolveAssemblyReference,它能够将短程序集名称和搜索提示转换为文件路径。您可以查看它在
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets中的使用方式

如果没有ResolveAssemblyReference,您可以做的最简单的事情就是这样编写:

<PropertyGroup> 
    <AssemblyName>MyAssemblyName</AssemblyName> 
    <OutputPath>bin\</OutputPath> 
</PropertyGroup> 

<ItemGroup> 
     <Compile Include="SomeSourceFile.cs" /> 
</ItemGroup> 

<ItemGroup> 
    <Reference Include="lib\Newtonsoft.Json.dll" />
</ItemGroup> 

<Target Name="Build"> 
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> 
    <Csc Sources="@(Compile)" References="@(Reference)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" /> 
</Target> 

MyAssemblyName
垃圾桶
请注意,引用项指定指向引用部件的直接路径。

您所做的是重载通常通过Microsoft.CSharp.targets导入的默认生成目标。在默认构建目标中,它接受.cs源文件所在的项数组@(Compile)和@(Reference)数组,以及其他内容,并合成对C#编译器的正确调用。您在自己的最小构建目标中没有做过这样的事情,它实际上忽略了@(引用)的声明,只向Csc任务提供@(编译)


尝试将References=“@(References)”属性添加到Csc任务。

如何使用msbuild引用完整的bin目录?@Gags,您可能需要为此创建一个新问题。