在'system'中找不到类型或命名空间名称'linq'

时间:2013-04-01 13:09:15

标签: c# msbuild

我正在尝试使用Microsoft Build Engine(也称为MSBuild)了解Bulding C#项目的过程,我面临着问题。问题很简单,认为我只是不明白。

我写了一个简单的程序,包含2个.cs文件。 第一个文件是“MathOp.cs”。在这个文件中我定义了2个函数:add(double num1,double num2)和multiply(doble,double); 第二个文件是“Program.cs”。在这里,我定义了两个变量,我传递给添加放在MathOp文件中的函数并获得rezult; 这个程序是正确的。

然后我写了一个简单的msbuild文件,我在其中定义了构建的任务和目标。 当我在visual studio命令提示符下启动它时,我得到了erorr cs0234:在System命名空间中找不到类型或命名空间名称Linq。我在msbuild文件中引用System.dll等最有趣的。如果我在Program.cs文件中注释using指令,那么这个错误就会消失。

<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup>
    <AssemblyInfo>Build</AssemblyInfo>
    <builtdir>Build\</builtdir>
</PropertyGroup>

<ItemGroup>
    <CSFile Include="msbuildTest\Program.cs"/>
    <CSFile Include="msbuildTest\Properties\AssemblyInfo.cs"/>
    <CSFile Include="msbuildTest\MathOp.cs"/>

    <Reference Include="System.dll"/>
    <Reference Include="System.Data.dll"/>
    <Reference Include="System.Drawing.dll"/>
    <Reference Include="System.Windows.Forms.dll"/>
    <Reference Include="System.XML.dll"/>
</ItemGroup>

<Target Name="PreBuild">
    <Exec Command="if not exist $(builtdir) md $(builtdir)"/>
</Target>

<Target Name="Compile" DependsOnTargets="PreBuild">
    <Csc Sources="@(CSFile)"
        References="@(Reference)"
        OutputAssembly="$(builtdir)$(MSBuildProjectName).exe"
        TargetType="exe"/>
</Target>

<Target Name="Clean" >
    <Exec Command="DEL $(builtdir)$(AssemblyInfo).exe"/>
</Target>
<Target Name="Rebuild" DependsOnTargets="Clean;Compile"/>

1 个答案:

答案 0 :(得分:2)

在当前状态下,msbuild将使用来自.Net 2.0 的CSC,后者对Linq一无所知。

解决问题的最简单方法是在msbuild项目中指定4.0版本,从而迫使msbuild使用正确的CSC版本:

<Project ToolsVersion="4.0"
    DefaultTargets="Compile"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
相关问题