简体   繁体   中英

the type or namespace name 'linq' could not be found in 'system'

I'm trying to understand the process of Bulding C# project using Microsoft Build Engine (also known as MSBuild) and I face to problem. The problem is simple, think I just don`t understand something.

I wrote a simple program consist from 2 .cs files. First file is "MathOp.cs". In this file I define 2 functions: add(double num1, double num2) and multiply(doble, double); Second file is "Program.cs." Here I define two variable which I passed to add function placed in MathOp file and getting rezult; This programm is correct.

Then I wrote a simple msbuild file where I define the tasks and targets for building. And when i launch it in visual studio command prompt I getting the erorr cs0234: the type or namespace name Linq could not be found in System namespace. The most interesting that I referenced to the System.dll and etc. in msbuild file. And if I comment the using directives in Program.cs file this error is disappear.

<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"/>

In current state msbuild will use CSC from .Net 2.0 , which know nothing about Linq.

The easiest way to fix the issue it to specify 4.0 version in msbuild project thus forcing msbuild to use correct CSC version:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM