继承问题基础与子代

本文关键字:问题 继承 | 更新日期: 2023-09-27 18:28:26

我正在编写一个套接字服务器类,其他类将从派生该类

当我从这个类派生,试图创建一个Web服务器时,基类事件就在那里。我不希望它们在从Web服务器派生的其他类中可见,也不希望最终用户使用Web服务器类。我可以以某种方式隐藏它们以供进一步使用,但仍然可以订阅它们。因为在这一点上,我脑子里唯一的办法就是在我的基础上重新发明课堂上的轮子!

下面是基类的一些代码:

    public delegate void ConnectionRequest (CoreAsyncSocketObj client);
    public delegate void DataReceived (CoreAsyncSocketObj client, string data);
    public delegate void DataSent (CoreAsyncSocketObj client);
    public delegate void ConnectionClose (CoreAsyncSocketObj client);
    public delegate void ServerFull (CoreAsyncSocketObj client);
    /// <summary>
    /// Occurs when a client connects to server.
    /// </summary>
    public event ConnectionRequest OnConnectionRequest;
    /// <summary>
    /// Occurs when server receives data from any client.
    /// </summary>
    public event DataReceived OnDataReceived;
    /// <summary>
    /// Occurs when data has been sent to client.
    /// </summary>
    public event DataSent OnDataSent;
    /// <summary>
    /// Occurs when client has closed its connection.
    /// </summary>
    public event ConnectionClose OnConnectionClose;
    /// <summary>
    /// Occurs when server is full according to the MaximumUsers property.
    /// </summary>
    public event ServerFull OnServerFull;

以上就是我如何安排我的代表和活动!这个问题实际上是在我的代表回电话上,我称这些事件为

    void Receive (IAsyncResult ar)
    {
        CoreAsyncSocketObj client = (CoreAsyncSocketObj)ar.AsyncState;
        string data = string.Empty;
        try
        {
            int bytesReceived = client.sock.EndReceive (ar);
            if (bytesReceived > 0) // client is connected
            {
                if (this.protocolEOL == string.Empty) // If no delimeter then just raise event and restart receiving.
                {
                    if (this.OnDataReceived != null) 
                    {
                        data = Encoding.GetEncoding((int)this.encoder).GetString(client.buffer);
                        this.OnDataReceived (client, data);
                        client.sock.BeginReceive (client.buffer, 0, client.buffer.Length, SocketFlags.None,
                                                new AsyncCallback (Receive), client);
                    }
                } 
                else // A specified delimter (EOL).
                {
                    // Append to a second buffer using the specified encoding.
                    // Check the end of the buffer if it matches the EOL.
                    client.stringBuffer.Append(Encoding.GetEncoding((int)this.encoder).GetString(client.buffer));
                    //client.stringBuffer.Append (Encoding.Default.GetString (client.buffer));
                    data = client.stringBuffer.ToString ();
                    if (data.EndsWith (this.protocolEOL) == true) 
                    {
                        if (this.OnDataReceived != null)
                            this.OnDataReceived (client, data);
                        client.stringBuffer.Clear(); // Clear buffer
                    }
                    client.sock.BeginReceive (client.buffer, 0, client.buffer.Length, SocketFlags.None,
                                              new AsyncCallback (Receive), client);  // restart
                }
            } 
            else // Client has closed its connection
            {
                this.DisconnectClient(client);
                if (this.OnConnectionClose != null)
                    this.OnConnectionClose(client);
            }
        }
        catch(SocketException exception)
        {
            Logger.Write(exception.Message + "'"" + exception.ErrorCode.ToString() + "'"");
        }
        catch(ObjectDisposedException exception)
        {
            Logger.Write(exception.Message);
        }
        catch(Exception exception)
        {
            Logger.Write(exception.Message);
        }
    }

如果我不能在派生该类的另一个类中订阅DataReceived事件,那么我该如何处理实际的核心?我的头撞到墙上了。也许从一开始就是一个糟糕的结构?

继承问题基础与子代

我认为没有办法完全隐藏继承的成员。类似问题"隐藏继承的成员"中的一个建议是覆盖它们,将其标记为oblete,并将DesignerSerializationVisibility设置为Hidden。

如果只想在程序集中使用这些成员,但将它们隐藏到外部程序集中,请将它们标记为internal而不是public

如果你真的想隐藏它们,你可以把你的web服务器类写成一个包装器,而不是一个子类。在内部使用套接字服务器类并订阅事件。