创建增量构建时出现 CS0246 错误
CS0246 error while creating incremental build
我制作了一个小型 C# 应用程序,其中包含 1 个解决方案和 2 个项目 (test1
、test2
)。第一个项目有两个文件 Program.cs
和 function1.cs
,而第二个项目只包含 Program2.cs
。
项目 test1
创建 .exe
文件,test2
创建 .dll
文件。
问题是当我尝试在 VS 创建 .csproj 时构建它 运行 没问题,但是当我尝试编辑该 .csproj (或编写我自己的)以实现增量构建时(就像在 MS 上的示例一样)网站)它抛出一个 CS0246
The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)
尽管我首先将引用添加到 test2.csproj
,然后添加到它创建的 .dll
,并且还使用了 using
指令。
当我尝试在命令提示符下 运行 时出现同样的错误
csc.exe program.cs function1.cs
但可以通过添加 /:r
和 .dll
来快速修复,例如:
\csc.exe program.cs function1.cs /r:test2.dll
然后 运行 没问题。我只是不知道如何确认文件之间引用的 msbuild。而且我也被迫使用 cmd 而不是在 VS 中构建它。
下面是我在项目中使用的代码。
这是我在 test1.csproj
末尾添加的片段,以强制其进入增量构建:
<ItemGroup>
<Compile Include="function1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Target Name="program2">
<MSBuild Projects="..\test2\test2.csproj" />
<Message Text="Target program 2" />
</Target>
<Target Name="Compile" Inputs="@(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
这是 Program.cs
的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test1
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
string pause;
function1 var = new function1();
a = Convert.ToInt32(Console.ReadLine());
b = var.funkcja1(a);
Console.WriteLine(b);
pause = Console.ReadLine();
}
}
}
这是function1.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using test2;
namespace test1
{
class function1
{
public int funkcja1(int a)
{
int b;
Program2 p2 = new Program2();
b = p2.program2(a);
return (b);
}
}
}
这里是 Program2.cs
的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test2
{
public class Program2
{
public class function2
{
public int funkcja2(int a)
{
return (a + 1);
}
}
public int program2(int c)
{
int d;
function2 var = new function2();
d = var.funkcja2(c) + 1;
return (d);
}
public static void Main(string[] args)
{
string pause;
pause = Console.ReadLine();
}
}
}
CS0246 error while creating incremental build
在使用csc.exe
构建时需要指定引用,如References="..\test2\bin\Debug\test2.dll"
,否则csc.exe无法知道test.dll被[=引用了32=],所以目标 Compile
看起来像:
<Target Name="Compile" Inputs="@(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" References="..\test2\bin\Debug\test2.dll" />
</Target>
此外,据我所知,您应该不 编辑该 .csproj(或编写我自己的)以直接实现增量构建。那是因为在构建项目test1时,VisualStudio/MSBuild已经默认使用了增量构建,我们不需要再进行增量构建。如果你想实现增量构建,可以create a mini MSBuild Project File实现增量构建。我在 test1 项目文件夹下创建了一个新的 Test.proj
,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="function1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup>
<AssemblyName>MSBuildTestSample</AssemblyName>
<OutputPath>Bin\</OutputPath>
</PropertyGroup>
<Target Name="program2">
<MSBuild Projects="..\test2\test2.csproj" />
<Message Text="Target program 2" />
</Target>
<Target Name="Compile" Inputs="@(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" References="..\test2\bin\Debug\test2.dll" />
</Target>
</Project>
然后我们可以使用 MSBuild 命令行构建这个 Test.proj
文件。
希望对您有所帮助。
我制作了一个小型 C# 应用程序,其中包含 1 个解决方案和 2 个项目 (test1
、test2
)。第一个项目有两个文件 Program.cs
和 function1.cs
,而第二个项目只包含 Program2.cs
。
项目 test1
创建 .exe
文件,test2
创建 .dll
文件。
问题是当我尝试在 VS 创建 .csproj 时构建它 运行 没问题,但是当我尝试编辑该 .csproj (或编写我自己的)以实现增量构建时(就像在 MS 上的示例一样)网站)它抛出一个 CS0246
The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)
尽管我首先将引用添加到 test2.csproj
,然后添加到它创建的 .dll
,并且还使用了 using
指令。
当我尝试在命令提示符下 运行 时出现同样的错误
csc.exe program.cs function1.cs
但可以通过添加 /:r
和 .dll
来快速修复,例如:
\csc.exe program.cs function1.cs /r:test2.dll
然后 运行 没问题。我只是不知道如何确认文件之间引用的 msbuild。而且我也被迫使用 cmd 而不是在 VS 中构建它。
下面是我在项目中使用的代码。
这是我在 test1.csproj
末尾添加的片段,以强制其进入增量构建:
<ItemGroup>
<Compile Include="function1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Target Name="program2">
<MSBuild Projects="..\test2\test2.csproj" />
<Message Text="Target program 2" />
</Target>
<Target Name="Compile" Inputs="@(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
这是 Program.cs
的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test1
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
string pause;
function1 var = new function1();
a = Convert.ToInt32(Console.ReadLine());
b = var.funkcja1(a);
Console.WriteLine(b);
pause = Console.ReadLine();
}
}
}
这是function1.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using test2;
namespace test1
{
class function1
{
public int funkcja1(int a)
{
int b;
Program2 p2 = new Program2();
b = p2.program2(a);
return (b);
}
}
}
这里是 Program2.cs
的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test2
{
public class Program2
{
public class function2
{
public int funkcja2(int a)
{
return (a + 1);
}
}
public int program2(int c)
{
int d;
function2 var = new function2();
d = var.funkcja2(c) + 1;
return (d);
}
public static void Main(string[] args)
{
string pause;
pause = Console.ReadLine();
}
}
}
CS0246 error while creating incremental build
在使用csc.exe
构建时需要指定引用,如References="..\test2\bin\Debug\test2.dll"
,否则csc.exe无法知道test.dll被[=引用了32=],所以目标 Compile
看起来像:
<Target Name="Compile" Inputs="@(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" References="..\test2\bin\Debug\test2.dll" />
</Target>
此外,据我所知,您应该不 编辑该 .csproj(或编写我自己的)以直接实现增量构建。那是因为在构建项目test1时,VisualStudio/MSBuild已经默认使用了增量构建,我们不需要再进行增量构建。如果你想实现增量构建,可以create a mini MSBuild Project File实现增量构建。我在 test1 项目文件夹下创建了一个新的 Test.proj
,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="function1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup>
<AssemblyName>MSBuildTestSample</AssemblyName>
<OutputPath>Bin\</OutputPath>
</PropertyGroup>
<Target Name="program2">
<MSBuild Projects="..\test2\test2.csproj" />
<Message Text="Target program 2" />
</Target>
<Target Name="Compile" Inputs="@(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" References="..\test2\bin\Debug\test2.dll" />
</Target>
</Project>
然后我们可以使用 MSBuild 命令行构建这个 Test.proj
文件。
希望对您有所帮助。