Ton*_*Nam 9 c# compiler-errors compilation .net-core
我正在编辑我的问题,我认为这有点令人困惑,并且不能解释我的意图。
我的目标是,当我的HelloWorld应用程序引用MyClassLibrary我的代码时,不会编译它们,因此我确保在运行main方法之前先初始化一些代码。有点像类的构造函数。当我引用MyClassLibrary时,在运行HelloWorld应用程序的main方法之前,我希望在其中运行一些代码。NUnit具有类似的功能。当我的HelloWorld应用程序引用NUnit时,出现错误:Error CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.@Alex指出NUnit创建的Main方法是自动生成的。我想用一些自定义代码自动生成main方法。像NUnit一样,如何不对应用程序MyClassLibrary执行任何操作就如何做到这HelloWorld一点?
我要执行与NUnit测试执行相同的行为,以防止使用某种Main方法。在这种情况下,我需要的错误是一件好事。让我解释一下我的意思。
.net core项目文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
代码文件:(默认的Hello World C#代码)
如果我随后运行该应用程序,它将运行正常
添加引用NUnit,我的项目文件现在包含。
。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
错误CS0017程序定义了多个入口点。用/ main编译以指定包含入口点的类型。
这意味着还有另一种Main方法。该方法可能位于NUnit我正在引用的nuget包上。这是我要复制的错误!
现在,这就是我尝试复制相同错误的方法:
我删除了在hello world应用NUnit程序中没有引用的块包NUnit。
ClassLibrary1使用以下代码创建一个项目:
。
public class MyLib
{
static void Main()
{
Console.WriteLine("fooooo");
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,即使有两种Main方法,我也不会出错!
如何NUnit避免使用Main方法?如何复制相同的行为?我想创建一个程序集,该程序集在引用时会阻止执行该Main方法。
Microsoft.NET.Test.Sdk making build fail.<GenerateProgramFile>false</GenerateProgramFile> into <PropertyGroup> makes it compile and work anyway.static void Main to the application makes build fail again regardless <GenerateProgramFile>.Microsoft.NET.Test.Sdk adds some auto-generated code to your application before compilation. That code is in ...\.nuget\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs. It's a class with another Main:// <auto-generated> This file has been auto generated. </auto-generated>
using System;
[Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode]
class AutoGeneratedProgram {static void Main(string[] args){}}
Run Code Online (Sandbox Code Playgroud)
BTW: it's absolutely legal to have
Mainmethod in another assembly. You just cannot have 2Mains in one exe. But you can have any number of them in dll like this:
public class Class1
{
public static void Main() { }
public static void Main(string[] args) { }
}
public class Class2
{
public static void Main() { }
public static void Main(string[] args) { }
}
Run Code Online (Sandbox Code Playgroud)
It compiles.
Update:
I found the solution. It's all about installing nuget, not just adding a reference.
.NET Core Class Library and name it MyCoreLib.MyCoreClass.namespace MyCoreLib
{
public static class MyCoreClass
{
public static void Initialize()
{
System.Console.WriteLine("Initialized from 'MyCoreLib'");
}
}
}
Run Code Online (Sandbox Code Playgroud)
????nuget
????src
? MyCoreLib.nuspec
?
????build
? ????netcoreapp2.1
? ForcedEntryPoint.cs
? MyCoreLib.targets
?
????lib
????netcoreapp2.1
MyCoreLib.dll
Run Code Online (Sandbox Code Playgroud)
MyCoreLib.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>MyCoreLib</id>
<version>1.0.0</version>
<authors>MyCoreLib</authors>
<owners>MyCoreLib</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Some description here</description>
<dependencies>
<group targetFramework=".NETCoreApp2.1" />
</dependencies>
</metadata>
</package>
Run Code Online (Sandbox Code Playgroud)
ForcedEntryPoint.cs
//??????????????????????????????????????
//? This code was added automatically. ?
//? Do not change or remove it. ?
//??????????????????????????????????????
public static class ForcedEntryPoint
{
public static void Main(string[] args)
{
MyCoreLib.MyCoreClass.Initialize();
}
}
Run Code Online (Sandbox Code Playgroud)
MyCoreLib.targets
<Project InitialTargets="ForceEntryPoint" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<ForcedEntryPoint Condition="'$(ForcedEntryPoint)' == ''">$(MSBuildThisFileDirectory)ForcedEntryPoint$(DefaultLanguageSourceExtension)</ForcedEntryPoint>
<ForceEntryPoint Condition="'$(ForceEntryPoint)' == ''">true</ForceEntryPoint>
</PropertyGroup>
<Target Name="ForceEntryPoint" Condition="'$(ForceEntryPoint)' == 'true'">
<ItemGroup>
<Compile Include="$(ForcedEntryPoint)"/>
</ItemGroup>
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud)
D:\nugetwalkthrough\nuget>D:\nugetwalkthrough\nuget.exe pack D:\nugetwalkthrough\src\MyCoreLib.nuspec
Run Code Online (Sandbox Code Playgroud)
.NET Core Console App and make sure it works.CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
Main method from the application, run it and see it prints Initialized from 'MyCoreLib'.Main method back to the application and change the project file so that <PropertyGroup> contains <ForceEntryPoint>false</ForceEntryPoint>Hello World! from its own Main method.<ForceEntryPoint> to true makes it use another entry point (not that one of the application) again.| 归档时间: |
|
| 查看次数: |
350 次 |
| 最近记录: |