2017-09-06 83 views
-2

如何.NetStandard2下面寫代碼,因此它不會返回null的typeof(System.Net.WebUtility).GetMethod返回`null`爲.NetStandard2(正常工作與.NetFramework4.6.1)

它正常工作與.NetFramework4.6.1

MethodInfo method = typeof(System.Net.WebUtility).GetMethod(nameof(System.Net.WebUtility.UrlEncode), 
     BindingFlags.Static | BindingFlags.NonPublic, 
     null, 
     new[] 
     { 
      typeof(byte[]), 
      typeof(int), 
      typeof(int) 
     }, 
     null); 

尋找this line是準確的,但一個是.NetFramework4.7,找不到System.Net.WebUtilityCoreFx都沒有。

+1

我的隨機猜測是,.NetStandard和Framework之間的參數是不同的? – CShark

+0

請解釋你爲什麼投票! – cilerler

+0

@CShark遺憾的是無法在[CoreFx](https://github.com/dotnet/corefx/tree/master/src)中找到'System.Net.WebUtility',但引用有簽名。 – cilerler

回答

2

的代碼其實是在問一個在System.Net.WebUtility命名的方法命名UrlEncode

  1. static
  2. 可以是非public(當你的鏈接顯示,實際上private
  3. 接受3個參數:byte[]intint

But .NET Standard 2.0 doesn't define any such method。因此,符合.NET標準2.0的實現(包括.NET Core 2.0)可以免費實現。這正是corefx所做的。 System.Net.WebUtilities的corefx實現中沒有這種方法:https://github.com/dotnet/corefx/blob/master/src/System.Runtime.Extensions/src/System/Net/WebUtility.cs

您是否考慮過使用UrlEncodeToBytes(byte[], int, int)而不是?它存在於.NET標準2.0中,並且應該在.NET Framework 4.6.1以及.NET Core 2.0中受支持。

+0

謝謝!根據https://apisof.net/catalog/System.Web.HttpUtility.UrlEncodeToBytes(Byte(),Int32,Int32)它不應該在System.Runtime.Extensions下。我感謝您的幫助。 – cilerler

相關問題