简体   繁体   English

名称空间中不存在类型或命名空间名称“GetName”

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

The following code runs in a simple demo but it doesn't compile in my real project. 以下代码在一个简单的演示中运行,但它不能在我的实际项目中编译。

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))); 
        }
    }
}

The error message is: 错误消息是:

The type or namespace name 'GetName' does not exist in the namespace 'UtilLibrary.Enum' 命名空间“UtilLibrary.Enum”中不存在类型或命名空间名称“GetName”

If I change the top namespace in the file (in real project) from UtilLibrary to something else like UtilityLibrary , it compiles fine. 如果我将文件中的顶级命名空间(在实际项目中)从UtilLibrary为其他类似UtilityLibrary ,它编译得很好。 It seems like there would be a conflict with GetName() but there is not, nothing comes up in search. 似乎与GetName()存在冲突,但没有,搜索中没有任何内容。

So how can I stay in the same namespace and fix the compile error? 那么我如何保持相同的命名空间并修复编译错误?

ps I am new to c#. ps我是c#的新手。 Also if add dot to change the namespace like UtilLibrary.General , that still doesn't fix the error. 此外,如果添加点来更改像UtilLibrary.General这样的命名空间,那仍然无法修复错误。

Even though it's not best practice, you can always specify an alias to solve naming issues 尽管这不是最佳实践,但您始终可以指定别名来解决命名问题

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

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

Remember that it is usally better to fix the naming, as long as it is your code. 请记住,只要是您的代码,修复命名通常会更好。

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

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