[英]Cannot implicitly convert type 'System.Version' to 'System.Net.HttpVersion'
Consider the following code: 请考虑以下代码:
void Main()
{
HttpVersion version;
string s = "HTTP/1.1";
version = s == "HTTP/1.1" ? HttpVersion.Version11 : HttpVersion.Version10;
}
This throws the error 这会引发错误
Cannot implicitly convert type 'System.Version' to 'System.Net.HttpVersion'
无法将类型'System.Version'隐式转换为'System.Net.HttpVersion'
even though System.Version
is nowhere inferred and isn't even part of HttpVersion
's inheritance . 即使
System.Version
无处推断,也不是HttpVersion
继承的一部分 。
Explicit casts 明确的演员表
void Main()
{
HttpVersion version;
string s = "HTTP/1.1";
version = s == "HTTP/1.1" ? (HttpVersion) HttpVersion.Version11 : (HttpVersion) HttpVersion.Version10;
}
or explicit namespace naming 或显式命名空间命名
void Main()
{
System.Net.HttpVersion version;
string s = "HTTP/1.1";
version = s == "HTTP/1.1" ? System.Net.HttpVersion.Version11 : System.Net.HttpVersion.Version10;
}
don't make a difference. 不要有所作为。
The question is simple: why is System.Version
interfering with what should be an unrelated class? 问题很简单:为什么
System.Version
干扰应该是一个不相关的类?
The Version10
and Version11
static fields of HttpVersion
return System.Version
objects, not HttpVersion
objects. HttpVersion
的Version10
和Version11
静态字段返回System.Version
对象,而不是HttpVersion
对象。 So it's not technically unrelated. 所以它在技术上并不相关。
Now as for WHY it does that, I have absolutely no idea. 至于为什么这样做,我完全不知道。 But you'll note that the places where it is used (such as
HttpWebRequest.ProtocolVersion
) use System.Version
as well. 但是你会注意到它的使用位置(例如
HttpWebRequest.ProtocolVersion
)也使用System.Version
。 So that's what you want to be using. 这就是你想要使用的东西。
To me, it kind of looks kind of like HttpVersion
should have been a static class as it has no instance members that are not inherited from Object
... 对我来说,它有点像
HttpVersion
应该是一个静态类,因为它没有没有从Object
继承的实例成员...
HttpVersion.Version11
和HttpVersion.Version10
都返回一个Version
对象,您试图将其存储在HttpVersion
变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.