.net 4.0和4.5不同版本的Uri.ToString行为不同?

会思考的草 2014-04-18 08:48:16
众所周知安装.NET 4.5会覆盖掉原来的.NET 4.0,所以装完.NET 4.5之后,原来的.NET4.0实际上就在机器上不存在了。

问题1,为什么VS里面在创建项目的时候依然可以选择.net 4.0或者.net 4.5?
根据上面的结论,装完.net 4.5之后,不管创建项目的时候选4.0还是4.5,最终加载的都是4.5的dll。为了验证,我分别创建了两个工程,一个选择4.0一个选择4.5,分别在代码里用Assembly.GetAssembly看了一下,两个工程实际上调用的是同一个System.dll:
CodeBase = "file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll",等于是证明了我的推论,那么VS里面提供4.0和4.5的选项意义何在?

问题2,既然证明了创建项目的时候选择4.0也好,4.5也好,最终调用的都是4.5的assembly,那么按理说同一份代码的行为是一样的,那么为什么Uri.ToString返回的结果会大相径庭?代码如下:

string strWctx = @"https://www.mysite.com/login.aspx?idp=orgid&accountid=4c0b2839-a372-4771-9627-2f370fe10bed&B=P%3D//alerts/alert/%26A%3D%7BAL%3D2%7D&P=App.aspx";

UriBuilder builder = new UriBuilder(strWctx);
Uri someUri = builder.Uri;
string x = someUri.ToString();


指定.net 4.0的工程,Uri.ToString返回结果是(注意%3D,%26,%7B,%7D均已解码):
x = "https://www.mysite.com/login.aspx?idp=orgid&accountid=4c0b2839-a372-4771-9627-2f370fe10bed&B=P=//alerts/alert/&A={AL=2}&P=App.aspx"


指定.net 4.5的工程,Uri.ToString返回结果是(注意仅仅解码了%7B,%7D):
x="https://www.mysite.com/login.aspx?idp=orgid&accountid=4c0b2839-a372-4771-9627-2f370fe10bed&B=P%3D//alerts/alert/%26A%3D{AL%3D2}&P=App.aspx"


这两个返回结果并不一样,这和前面的结论产生了矛盾:既然调用的是同一份system.dll,如何产生不同的行为?
...全文
526 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
会思考的草 2014-04-23
  • 打赏
  • 举报
回复
引用 3 楼 feiyun0112 的回复:
跟踪源码后发现是由这个决定的 UriParser.s_QuirksVersion = (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? UriParser.UriQuirksVersion.V3 : UriParser.UriQuirksVersion.V2);
谢谢,我关注的不是.NET怎么实现的,而是为什么要这样做。 看上去和RFC 2396,3986和3987有关,.NET 4.5开始支持RFC 3986: Uri.EscapeDataString , Uri.EscapeUriString, and Uri.UnescapeDataString The list of reserved and unreserved characters now supports RFC 3986. Specific changes: •Unreserved escaped characters are un-escaped. •EscapeDataString escapes reserved characters based on RFC 3986. •EscapeUriString does not escape reserved characters. •UnescapeDataString does not throw an exception if it encounters an invalid escape sequence.
newxdlysk 2014-04-18
  • 打赏
  • 举报
回复
类似 #ifdefine NET4.0 这种吧
shawn_yang 2014-04-18
  • 打赏
  • 举报
回复
我的瞎猜哦,类似重载吧,编译是通过参数不同编译结果不同,例如4.0用参数1,4.5用参数2
feiyun0112 2014-04-18
  • 打赏
  • 举报
回复
跟踪源码后发现是由这个决定的
UriParser.s_QuirksVersion = (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? UriParser.UriQuirksVersion.V3 : UriParser.UriQuirksVersion.V2);

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧