我做了一个小的C#应用程序,有一个解决方案和两个项目(test1,test2)。第一个项目有两个文件Program.cs和function1.cs,而第二个项目只包含Program2.cs。项目test1创建.exe文件,test2创建.dll文件。问题是,当我尝试在VS创建.csproj时构建它,它会运行良好,但是当我试图编辑那个.csproj (或者编写自己的)以实现增量构建(就像在MS站点上的例子一样)时,它会抛出一个CS0246。
无法找到类型或名称空间名称“类型/命名空间”(您是缺少了使用指令还是程序集引用?)
尽管如此,我还是先添加了对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();
}
}
}发布于 2018-08-02 08:15:55
创建增量构建时发生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>此外,AFAIK,您应该而不是编辑该.csproj (或编写自己的)来直接实现增量构建。这是因为当您构建项目test1时,Visual /MSBuild默认情况下已经使用了增量构建,因此我们不需要再次增量构建它。如果要实现增量构建,可以通过创建一个小型MSBuild项目文件实现增量构建。我在Test.proj项目文件夹下创建了一个新的test1,代码如下:
<?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>然后,我们可以使用Test.proj命令行构建这个MSBuild文件。
希望这能有所帮助。
https://stackoverflow.com/questions/51631450
复制相似问题