继承问题基础与子级
Posted
技术标签:
【中文标题】继承问题基础与子级【英文标题】:Inheritance issue base vs child 【发布时间】:2014-12-26 23:04:55 【问题描述】:我写一个套接字服务器类其他类将来自 P>导出
当我从这个类派生,试图使一个网络服务器,基础类事件在那里。 我不希望他们看到在另一个班,从网络服务器,也没有使用Web服务器类最终用户获得。我会以某种方式能够隐藏他们进一步使用,但仍然可以进行订阅。因为在我的头在这一点的唯一方法是重新发明***的类,从我的基地导出! P>
下面如下的基类的一些代码:
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;
以上是我怎么有我的委托和事件! 真正的问题是在我的委托回调,在那里我把这些事件 P>
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
事件在我的其他类导出这个类,那么我能做些实际的核心是什么?我的头碰了壁。也许从一开始一个坏结构? P>
【问题讨论】:
如果你想在导出 I>类拥有的唯一通道,使用protected
访问修饰符。否则,我不知道你想要完成的任务。 SPAN>
【参考方案1】:
我不相信有办法完全隐藏继承的成员。来自类似问题“Hiding inherited members”的一个建议是覆盖它们,将它们标记为过时,并将DesignerSerializationVisibility
设置为隐藏。
如果您只想在程序集中使用这些成员但将它们隐藏到外部程序集中,请将它们标记为 internal
而不是 public
。
如果您真的想隐藏它们,您可以将您的 Web 服务器类编写为包装器而不是子类。在内部使用套接字服务器类并订阅事件。
【讨论】:
以上是关于继承问题基础与子级的主要内容,如果未能解决你的问题,请参考以下文章