繁体   English   中英

我的代码中出现以下错误和警告,我不确定为什么

[英]I am getting the following errors and warnings in my code and I am not sure as to why

我正在使用 Microsoft Visual Studio、C# 和 .NET。 这是我的代码

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

[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.");
        }
    }

当我尝试运行我的代码时,我收到一个公共错误,显示“CS0106:修饰符 'public' 对此项目无效。我还收到 OpenDrawing 警告,显示“CS8321:声明了本地函数 'OpenDrawing'但从未使用过。

我对使用 C# 很陌生,所以任何反馈都会有所帮助。 谢谢!

该方法被编写为顶级语句 方法文档是这样说的:

在使用顶级语句的应用程序中, Main方法由编译器生成并包含所有顶级语句。

这意味着OpenDrawing实际上是编译器生成的Main方法中的一个本地函数 这会导致错误,因为本地函数不能具有访问修饰符

这可以通过将方法放在一个类(最好是命名空间)中来解决:

namespace MyNamespace
{
    public class MyClass
    {
        // your OpenDrawing declaration here
    }
}

然后可以将其称为MyClass.OpenDrawing 该类应保存在与该类同名的自己的文件中(即示例中的MyClass.cs ),以保持代码的组织性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM