";使用名称空间“;DNS和AddressFamily上的问题

本文关键字:DNS AddressFamily 问题 空间 quot | 更新日期: 2023-09-27 18:19:48

我是C#的新手,通常我的强项是C++和VB。我有两个问题,我对代码发表了评论:

1.)蓝色波浪线出现在3上使用它表示"using namespace指令只能应用于命名空间;'System.Object"…"

2.)我无法从我声明为public的变量LocalIP中获得输出IP地址。

提前谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
//Blue squiggly line appear on this 2 Uses
using Dns = System.Net.Dns;
using AddressFamily = System.Net.Sockets.AddressFamily;

namespace WindowsFormsApplication1
{
public partial class frm_Log : Form
{
    public String localIP;
    public string LocalIPAddress()
    {
        IPHostEntry host;
        //string localIP = "";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
                break;
            }
        }
        return localIP;
    }
    ....
    ....
    // variable localIP.Trim() does not giveout any output
    MyValue="LogAccept,"+this.txt_UserName.Text.Trim()+","+this.txt_Password.Text.Trim() + "," + localIP.Trim() ;
    ....
    ....

";使用名称空间“;DNS和AddressFamily上的问题

System.Object是一个类。System是名称空间,您已经将其定义为文件顶部的第一个using

http://msdn.microsoft.com/en-US/library/sf0df423aspx建议使用以下语法:

using Dns = System.Net.Dns;

使用(using System.Linq;)的标准版本只能针对命名空间,而不能针对命名空间中的类。不过,如果您已经导入了整个命名空间,则不需要专门导入其中的任何内容

原始代码为

using System
....
using System.Object;
using System.Net.Dns;
using System.Net.Sockets.AddressFamily;
....

分辨率

//using System.Object --> since uses system already defined.
using Dns = System.Net.Dns;
using AddressFamily = System.Net.Sockets.AddressFamily;