Dns不包含“GetHostEntry”的定义

时间:2018-02-17 13:29:08

标签: linux powershell syntax mono .net-core

我来from powershellMono

thufir@dur:~/mono$ 
thufir@dur:~/mono$ ls
hello.cs
thufir@dur:~/mono$ 
thufir@dur:~/mono$ cat hello.cs 
using System;

public class HelloWorld
{
    static public void Main ()
    {
        Console.WriteLine ("Hello Mono World");
    }
}

thufir@dur:~/mono$ 
thufir@dur:~/mono$ mcs hello.cs 
thufir@dur:~/mono$ 
thufir@dur:~/mono$ mono hello.exe 
Hello Mono World
thufir@dur:~/mono$ 
thufir@dur:~/mono$ dotnet --version
2.1.4
thufir@dur:~/mono$ 
thufir@dur:~/mono$ lsb_release --all
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 17.10
Release:    17.10
Codename:   artful
thufir@dur:~/mono$ 

我希望做这样的事情powershell one-liner:

Resolve-DnsName -Name localhost -Type ANY | Format-Table -AutoSize

但来自Mono。到目前为止,这是what I have

thufir@dur:~/mono$ 
thufir@dur:~/mono$ ls
dns.cs  hello.cs
thufir@dur:~/mono$ 
thufir@dur:~/mono$ mcs dns.cs 
dns.cs(10,35): error CS0117: `Dns' does not contain a definition for `GetHostEntry'
dns.cs(5,14): (Location of the symbol related to previous error)
dns.cs(11,28): error CS0118: `System.Net.IPAddress' is a `type' but a `variable' was expected
dns.cs(11,28): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
Compilation failed: 3 error(s), 0 warnings
thufir@dur:~/mono$ 
thufir@dur:~/mono$ cat dns.cs 
using System.Net.Sockets;
using System.Net;
using System; 

public class Dns
{
    static public void Main ()
    {
        Console.WriteLine ("Hello Mono World");
        IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
        Console.WriteLine (IPAddress);  
    }
}

thufir@dur:~/mono$ 

我有dotnet的正确版本吗?进口是否正确?

我是否正确地使用正确的类型正确声明并实例化ipaddress变量?

1 个答案:

答案 0 :(得分:1)

您自己的类名Dns将覆盖System.Net命名空间。试试这个例子:

using System.Net.Sockets;
using System.Net;
using System;

public class MyDns
{
    static public void Main()
    {
           Console.WriteLine("Localhost: ");
           IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
           Console.WriteLine(ip);
    }
}