在Visual Studio 2012中使用Newton.jsoft(json.net)将C#转换为VB.NET时出现转



这段代码有什么问题:在Visual Studio 2012中使用Newton.jsoft(json.net)将C#转换为VB .NET时出错:原始代码:

 public static List<TradeInfo> GetTrades(BtcePair pair)
    {
        string queryStr = string.Format("https://btc-e.com/api/2/{0}/trades", BtcePairHelper.ToString(pair));
        return JArray.Parse(WebApi.Query(queryStr)).OfType<JObject>().Select(TradeInfo.ReadFromJObject).ToList();
    }

翻译后的代码:

Public Shared Function GetTrades(pair As BtcePair) As List(Of TradeInfo)
    Dim queryStr As String = String.Format("https://btc-e.com/api/2/{0}/trades", _
                                                BtcePairHelper.ToString(pair))
    Return JArray.Parse(WebApi.Query(queryStr)).OfType(Of JObject)().[Select](TradeInfo.ReadFromJObject).ToList()
End Function

错误:

错误 1 未为"公共共享函数 ReadFromJObject(o As Newtonsoft.Json.Linq.JObject) 作为 TradeInfo"的参数"o"指定参数。

调用函数 (vb):

Public Shared Function ReadFromJObject(o As JObject) As TradeInfo
    If o Is Nothing Then
        Return Nothing
    End If
 Return New TradeInfo() With 
  {.Amount = o.Value(Of Decimal)("amount"), _
    .Price = o.Value(Of Decimal)("price"), _
    .[Date] = UnixTime.ConvertToDateTime(o.Value(Of Long)("date")), _
    .Item = BtceCurrencyHelper.FromString(o.Value(Of String)("item")), _
    .PriceCurrency = BtceCurrencyHelper.FromString(o.Value(Of String)
    ("price_currency")), _
    .Tid = o.Value(Of Long)("tid"), _
    .Type = TradeInfoTypeHelper.FromString(o.Value(Of String)("trade_type")) _
    }
End Function

我认为你只需要一个"AddressOf",因为你实际上并没有调用'ReadFromJObject':

Public Shared Function GetTrades(ByVal pairlist() As BtcePair, Optional ByVal limit As Integer = 150) As Dictionary(Of BtcePair, List(Of TradeInfoV3))
    Dim tradeInfoListReader As Func(Of JContainer, List(Of TradeInfoV3)) = (Function(x) x.OfType(Of JObject)().Select(AddressOf TradeInfoV3.ReadFromJObject).ToList())
    Return MakeRequest(Of List(Of TradeInfoV3))("trades", pairlist, tradeInfoListReader, New Dictionary(Of String, String)() From {{ "limit", limit.ToString() }}, True)
End Function

首先,C# 代码返回字典,而 VB 代码返回列表。 其次,这两种方法具有不同的参数数量和类型。 我没有看过。

相关内容

  • 没有找到相关文章

最新更新