如何添加适合入口点的静态"Main"方法?



我在我的代码上得到一个错误,说"错误CS5001
程序不包含适合入口点的静态'Main'方法";我在c#编码使用微软Visual Studio和。net。这是我的代码。

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;



class projectsummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]

public static void OpenDrawing()

{
string strFileName = "C:\DRAFT.dwg";
DocumentCollection acDocMgr = Application.DocumentManager;
if (File.Exists(strFileName))
{
acDocMgr.Open(strFileName, false);
}
else
{
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName +
" does not exist.");
}
}
}

我不知道如何处理这个错误。谢谢你!

看看这篇文章和你之前的问题,让我们试着分析一下发生了什么。

  1. 您在Visual Studio中创建了一个新的控制台应用程序。您没有勾选"不使用顶级语句"。这给了你一个基本上是空的Program.cs文件(没有"Main")方法可见)。
  2. 你删除了给你的Hello World代码,然后去创建一个静态方法——你上一个问题中的代码。
  3. Damien_The_Unbeliever评论说,基于错误,您将方法放在"顶级语句"中;
  4. 你把你的方法(它仍然在Program.cs内)包装在一个类中,现在突然你得到一个不能找到入口点错误。

用户Ryan Pattillo发布了一个很好的解释原来的问题-你的方法是"自己"在Program.cs文件中。你应该遵循他们的建议,但你也应该确保这个类在它自己的文件中。

你应该得到这个:

Program.cs

// this is the entire contents of the file
using ConsoleApp1;
ProjectSummer.OpenDrawing();

ProjectSummer.cs

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
namespace ConsoleApp1
{
public class ProjectSummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
// ...
}
}
}

ConsoleApp1更改为项目名称。

你的应用程序的入口点,现在是唯一的文件,有"顶级语句",仍然是Program.cs,因此你修复了不能找到入口点错误。


你可以做的另一个调整,如果你是c#新手,可能会很有用,那就是根本不使用顶级语句。将Program.cs修改为:

namespace ConsoleApp1
{
internal static class Program
{
// this is your program's entry point
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
ProjectSummer.OpenDrawing();
}
}
}

ConsoleApp1更改为项目名称。

不能在进程外使用AutoCAD . net API。为了能够使用AutoCAD .NET库,您必须构建一个"类库"。项目(DLL)和NETLOAD这个DLL从运行AutoCAD过程。请参阅有关进程内与进程外的主题,您可以从另一个主题开始了解如何创建AutoCAD . net项目。