【问题标题】:winforms namespace order - 'Security' does not exist in the namespace 'AAA.System'winforms 命名空间顺序 - 命名空间“AAA.System”中不存在“Security”
【发布时间】:2012-05-16 16:23:31
【问题描述】:

我有一个名为 AAA.System 的项目,其中包含一些简单的基本功能,可供我们所有其他项目共享。这已经存在了大约 5 年左右,我们有许多引用它的项目都运行良好。

现在,我正在创建一个新项目(WCF 服务),但在命名空间解析方面遇到了一些问题。具体来说,我有这些用法:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Configuration.Install;
using System.Reflection;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Xml;

在某些时候,我有这行代码:

string fqdn = System.Net.Dns.GetHostName();

产生错误:

The type or namespace name 'Net' does not exist in the namespace 'AAA.System' (are you missing an assembly reference?)

因此,Visual Studio (2010) 似乎以某种方式试图解析为 AAA.System.Net.Dns.GetHostName() 而不是 System.Net.Dns.GetHostName(),但我没有任何使用声明表明使用 AAA.System。我在这里遗漏了什么指示编译器使用 AAA.System?

【问题讨论】:

    标签: .net winforms visual-studio visual-studio-2010


    【解决方案1】:

    您正在调用以下行的类的命名空间是否设置为AAA

    string fqdn = System.Net.Dns.GetHostName(); 
    

    例如

    namespace AAA
    {
        static void M()
        {
            string fqdn = System.Net.Dns.GetHostName(); 
        }
    }
    

    要修复它,您应该能够在行前加上 global::

    string fqdn = global::System.Net.Dns.GetHostName(); 
    

    或者只是使用指令向您添加System.Net

    【讨论】:

    • 否 - 新项目上的所有命名空间都是 AAA.AuthService
    • 哦,好的。因此,当一个命名空间被解析时,它首先从类的命名空间向上走,然后再查找 global:: ?我知道它会在当前命名空间中查找,但没有意识到它会爬上树。
    • 是的。在工作中,每个命名空间都以相同的前缀开头(例如EEE),我总是在需要时写Controls.MyControl 而不是EEE.Controls.MyControl
    猜你喜欢
    • 2011-10-19
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2014-05-09
    • 2012-06-19
    相关资源
    最近更新 更多