【问题标题】:Mono says 'System.Net.Dns.GetHostEntry(string)' is inaccessible due to its protection levelMono 说“System.Net.Dns.GetHostEntry(string)”由于其保护级别而无法访问
【发布时间】:2010-09-23 01:26:28
【问题描述】:

我现在正在上课,其中一些示例是用 C# 编写的。由于我的笔记本电脑运行 Linux,我在 Ubuntu 上使用 Mono 2.6.7。

我正在尝试编译以下代码:

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

/// <summary>
/// Example program showing simple TCP socket connections in C#.NET.
/// Rather than reading and writing byte arrays, this example show
/// how to use a stream reader in the client.
/// TCPSocketServer is the socket server.
/// <author>Tim Lindquist ASU Polytechnic Department of Engineering</author>
/// <version>September, 2009</version>
/// </summary>
public class TCPSocketServer {

  public static void Main (string [] args) {
    IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    TcpListener tcpl = new TcpListener(ipAddress, 9090);
    tcpl.Start();
    Console.WriteLine("TCPSocketServer waiting for connections on 9090");
    Socket sock = tcpl.AcceptSocket();
    string msg = "Hello Client";
    Byte[] msgBytes = System.Text.Encoding.ASCII.GetBytes(msg);
    sock.Send(msgBytes, msgBytes.Length, SocketFlags.DontRoute);
    Console.WriteLine("Message-Hello Client-sent to client.");
    tcpl.Stop();
    sock.Close();
  }
}

当我编译代码时,我得到:

/home/vivin/Projects/cst420/CSSockets/src/TCPSocketServer.cs(16,31): error CS0122: `System.Net.Dns.GetHostEntry(string)' is inaccessible due to its protection level
/usr/lib/mono/gac/System/1.0.5000.0__b77a5c561934e089/System.dll (Location of the symbol related to previous error)
ompilation failed: 1 error(s), 0 warnings

我是 C# 新手;这是我编译的第一个 C# 程序。我尝试在谷歌上搜索,但我没有得到很多关于这个问题的点击。这是 Mono 的问题吗?

【问题讨论】:

    标签: c# mono


    【解决方案1】:

    System.Net.Dns.GetHostEntry 是在 .NET Framework 2.0 中引入的。在 Mono 的实现中,它也存在于 2.0 之前的版本中,但标记为 internal 而不是 public。您似乎正在针对 .NET Framework 1.0 进行编译。

    来自/mcs/class/System/System.Net/Dns.cs

    #if NET_2_0
        public
    #else
        internal
    #endif
        static IPHostEntry GetHostEntry (string hostNameOrAddress)
        {
            // ...
    

    【讨论】:

    • 回想起来,“System/1.0.5000.0__b77a5c561934e089”应该是赠品:)
    • 就是这样!我正在使用mcs。当我使用gmcs 时,问题就消失了。谢谢!
    【解决方案2】:

    您使用的是gmcs 编译器吗?

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 2015-09-26
      • 2011-09-01
      • 1970-01-01
      • 2011-04-02
      • 2011-02-07
      • 2012-01-25
      相关资源
      最近更新 更多