簡體   English   中英

名稱空間中不存在類型或命名空間名稱“GetName”

[英]The type or namespace name 'GetName' does not exist in the namespace

以下代碼在一個簡單的演示中運行,但它不能在我的實際項目中編譯。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string AgentName = "Test";
            string SERVICE_NAME = "Agent";
            ServiceController sc = new ServiceController(AgentName);
            ServiceControllerStatus status = sc.Status;
            Debug.WriteLine(String.Format("Service {0} {1}", SERVICE_NAME, 
                // compile error this next line: 
                Enum.GetName(typeof(ServiceControllerStatus), sc.Status))); 
        }
    }
}

錯誤消息是:

命名空間“UtilLibrary.Enum”中不存在類型或命名空間名稱“GetName”

如果我將文件中的頂級命名空間(在實際項目中)從UtilLibrary為其他類似UtilityLibrary ,它編譯得很好。 似乎與GetName()存在沖突,但沒有,搜索中沒有任何內容。

那么我如何保持相同的命名空間並修復編譯錯誤?

ps我是c#的新手。 此外,如果添加點來更改像UtilLibrary.General這樣的命名空間,那仍然無法修復錯誤。

盡管這不是最佳實踐,但您始終可以指定別名來解決命名問題

using SysEnum = System.Enum;
using LibEnum = Some.Custom.Library.Enum;

//Usage
SysEnum.GetName(typeof(ServiceControllerStatus), sc.Status)));
LibEnum.WhateverMethodIsImplementedHere();

請記住,只要是您的代碼,修復命名通常會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM