C# 覆盖';时更改访问修饰符;公共';继承成员
我有两个脚本,即HTTPReponse.cs和HTTPProxyReponse.cs,它们继承到HTTPResponse.cs HTTPResponse.csC# 覆盖';时更改访问修饰符;公共';继承成员,c#,unity3d,C#,Unity3d,我有两个脚本,即HTTPReponse.cs和HTTPProxyReponse.cs,它们继承到HTTPResponse.cs HTTPResponse.cs public virtual bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = true) { string statusLine = string.Empty; if (HTTPManager.Logg
public virtual bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = true)
{
string statusLine = string.Empty;
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Receive. forceReadRawContentLength: '{0:N0}', readPayloadData: '{1:N0}'", forceReadRawContentLength, readPayloadData));
// On WP platform we aren't able to determined sure enough whether the tcp connection is closed or not.
// So if we get an exception here, we need to recreate the connection.
try
{
// Read out 'HTTP/1.1' from the "HTTP/1.1 {StatusCode} {Message}"
statusLine = ReadTo(Stream, (byte)' ');
}
catch
{
if (!baseRequest.DisableRetry)
{
HTTPManager.Logger.Warning("HTTPResponse", string.Format("{0} - Failed to read Status Line! Retry is enabled, returning with false.", this.baseRequest.CurrentUri.ToString()));
return false;
}
HTTPManager.Logger.Warning("HTTPResponse", string.Format("{0} - Failed to read Status Line! Retry is disabled, re-throwing exception.", this.baseRequest.CurrentUri.ToString()));
throw;
}
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Status Line: '{0}'", statusLine));
if (string.IsNullOrEmpty(statusLine))
{
if (!baseRequest.DisableRetry)
return false;
throw new Exception("Remote server closed the connection before sending response header!");
}
string[] versions = statusLine.Split(new char[] { '/', '.' });
this.VersionMajor = int.Parse(versions[1]);
this.VersionMinor = int.Parse(versions[2]);
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("HTTP Version: '{0}.{1}'", this.VersionMajor.ToString(), this.VersionMinor.ToString()));
int statusCode;
string statusCodeStr = NoTrimReadTo(Stream, (byte)' ', LF);
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Status Code: '{0}'", statusCodeStr));
if (baseRequest.DisableRetry)
statusCode = int.Parse(statusCodeStr);
else if (!int.TryParse(statusCodeStr, out statusCode))
return false;
this.StatusCode = statusCode;
if (statusCodeStr.Length > 0 && (byte)statusCodeStr[statusCodeStr.Length - 1] != LF && (byte)statusCodeStr[statusCodeStr.Length - 1] != CR)
{
this.Message = ReadTo(Stream, LF);
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Status Message: '{0}'", this.Message));
}
else
{
HTTPManager.Logger.Warning("HTTPResponse", string.Format("{0} - Skipping Status Message reading!", this.baseRequest.CurrentUri.ToString()));
this.Message = string.Empty;
}
//Read Headers
ReadHeaders(Stream);
IsUpgraded = StatusCode == 101 && (HasHeaderWithValue("connection", "upgrade") || HasHeader("upgrade"));
if (IsUpgraded && HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging("Request Upgraded!");
if (!readPayloadData)
return true;
return ReadPayload(forceReadRawContentLength);
}
public class HTTPProxyResponse : HTTPResponse
{
internal override bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = false)
{
return base.Receive(forceReadRawContentLength, false);
}
}
HTTPProxyResponse.cs
public virtual bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = true)
{
string statusLine = string.Empty;
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Receive. forceReadRawContentLength: '{0:N0}', readPayloadData: '{1:N0}'", forceReadRawContentLength, readPayloadData));
// On WP platform we aren't able to determined sure enough whether the tcp connection is closed or not.
// So if we get an exception here, we need to recreate the connection.
try
{
// Read out 'HTTP/1.1' from the "HTTP/1.1 {StatusCode} {Message}"
statusLine = ReadTo(Stream, (byte)' ');
}
catch
{
if (!baseRequest.DisableRetry)
{
HTTPManager.Logger.Warning("HTTPResponse", string.Format("{0} - Failed to read Status Line! Retry is enabled, returning with false.", this.baseRequest.CurrentUri.ToString()));
return false;
}
HTTPManager.Logger.Warning("HTTPResponse", string.Format("{0} - Failed to read Status Line! Retry is disabled, re-throwing exception.", this.baseRequest.CurrentUri.ToString()));
throw;
}
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Status Line: '{0}'", statusLine));
if (string.IsNullOrEmpty(statusLine))
{
if (!baseRequest.DisableRetry)
return false;
throw new Exception("Remote server closed the connection before sending response header!");
}
string[] versions = statusLine.Split(new char[] { '/', '.' });
this.VersionMajor = int.Parse(versions[1]);
this.VersionMinor = int.Parse(versions[2]);
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("HTTP Version: '{0}.{1}'", this.VersionMajor.ToString(), this.VersionMinor.ToString()));
int statusCode;
string statusCodeStr = NoTrimReadTo(Stream, (byte)' ', LF);
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Status Code: '{0}'", statusCodeStr));
if (baseRequest.DisableRetry)
statusCode = int.Parse(statusCodeStr);
else if (!int.TryParse(statusCodeStr, out statusCode))
return false;
this.StatusCode = statusCode;
if (statusCodeStr.Length > 0 && (byte)statusCodeStr[statusCodeStr.Length - 1] != LF && (byte)statusCodeStr[statusCodeStr.Length - 1] != CR)
{
this.Message = ReadTo(Stream, LF);
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging(string.Format("Status Message: '{0}'", this.Message));
}
else
{
HTTPManager.Logger.Warning("HTTPResponse", string.Format("{0} - Skipping Status Message reading!", this.baseRequest.CurrentUri.ToString()));
this.Message = string.Empty;
}
//Read Headers
ReadHeaders(Stream);
IsUpgraded = StatusCode == 101 && (HasHeaderWithValue("connection", "upgrade") || HasHeader("upgrade"));
if (IsUpgraded && HTTPManager.Logger.Level == Logger.Loglevels.All)
VerboseLogging("Request Upgraded!");
if (!readPayloadData)
return true;
return ReadPayload(forceReadRawContentLength);
}
public class HTTPProxyResponse : HTTPResponse
{
internal override bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = false)
{
return base.Receive(forceReadRawContentLength, false);
}
}
这给了我一个错误的说法
重写“public”继承成员时无法更改访问修饰符
如何修复此错误?在派生类型中更改访问修饰符是毫无意义的,是不允许的
在
HTTPProxyResponse
public override bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = false)
将
内部
更改为公共
。内部比公共更严格。您是否尝试将HTTPProxyResponse中的接收方法标记为公共
,而不是内部
?您无法更改覆盖方法的访问修饰符。想象一下,将一个对象转换回它的基类型,然后您将神奇地能够访问以前的内部方法。好了,您只能增加重写方法的可见性(尽管C#不支持,所以您必须使用IL或其他支持该方法的语言),但不能减少它。谢谢。4分钟后,我才能接受这个作为answer@Ketskie谢谢你