简体   繁体   English

无法将类型'System.Version'隐式转换为'System.Net.HttpVersion'

[英]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. HttpVersionVersion10Version11静态字段返回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.Version11HttpVersion.Version10都返回一个Version对象,您试图将其存储在HttpVersion变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型隐式转换为system.net - Cannot Implicitly Convert Type to system.net System.Version未序列化 - System.Version not serialized 无法将类型 ('string', 'string') 隐式转换为 System.Net.ICredentialsByHost - Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost 无法将类型隐式转换为System.Net.Authorization? - Cannot implicitly convert type to System.Net.Authorization? 为什么.NET System.Version "2.0" 与 "2.0.0.0" 不同? - Why is .NET System.Version "2.0" different from "2.0.0.0"? 无法将类型“system.net.http.httpresponsemessage”隐式转换为“system.web.httpresponse” - cannot implicitly convert type 'system.net.http.httpresponsemessage' to 'system.web.httpresponse' 不能隐式转换类型System.DateTime? 到System.DateTime - cannot implicitly convert type System.DateTime? to System.DateTime 无法将类型“System.Web.Http.Results.BadRequestErrorMessageResult”隐式转换为“System.Net.Http.HttpResponseMessage” - Cannot implicitly convert type 'System.Web.Http.Results.BadRequestErrorMessageResult' to 'System.Net.Http.HttpResponseMessage' 如何在实体框架6中将System.Version映射为复杂类型 - How to map System.Version as Complex Type in Entity Framework 6 无法将类型 Android.Net.Uri 隐式转换为 System.Uri - Cannot implicitly convert type Android.Net.Uri to System.Uri
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM