C# CS0246创建增量生成时出错
我制作了一个小型C#应用程序,其中包括1个解决方案和2个项目(C# CS0246创建增量生成时出错,c#,msbuild,visual-studio-2017,incremental-build,C#,Msbuild,Visual Studio 2017,Incremental Build,我制作了一个小型C#应用程序,其中包括1个解决方案和2个项目(test1,test2)。第一个项目有两个文件Program.cs和function1.cs,而第二个项目只有Program2.cs。 项目test1创建.exe文件,而test2创建.dll文件。 问题是,当我尝试以VS creates.csproj的形式构建它时,它运行良好,但当我尝试编辑该.csproj(或编写我自己的)以实现增量构建(就像在MS站点上的示例一样)时,它抛出一个CS0246 找不到类型或命名空间名称“type/n
test1
,test2
)。第一个项目有两个文件Program.cs
和function1.cs
,而第二个项目只有Program2.cs
。
项目test1
创建.exe
文件,而test2
创建.dll
文件。
问题是,当我尝试以VS creates.csproj的形式构建它时,它运行良好,但当我尝试编辑该.csproj(或编写我自己的)以实现增量构建(就像在MS站点上的示例一样)时,它抛出一个CS0246
找不到类型或命名空间名称“type/namespace”(是否缺少using指令或程序集引用?)
尽管如此,我还是先将引用添加到test2.csproj
,然后添加到它创建的.dll
,并使用using
指令
尝试在命令提示符下运行时也会出现相同的错误
csc.exe program.cs function1.cs
,但可以通过使用.dll
添加/:r
快速修复,如示例所示:
\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>
下面是function1.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();
}
}
}
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);
}
}
}
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();
}
}
}
下面是Program2.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();
}
}
}
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);
}
}
}
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创建增量生成时出错
当您使用csc.exe
构建它时,应该指定引用,如References=“..\test2\bin\Debug\test2.dll”
,否则,csc.exe无法知道test1.project引用了test.dll,因此目标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>
然后我们可以使用MSBuild命令行构建这个Test.proj
文件
希望这有帮助。谢谢,添加引用修复了错误,我将使用另一个文件。