什么是以下

时间:2017-07-27 13:31:21

标签: c# vb.net delegates

我需要将以下内容转换为VB.Net。我尝试了在线转换器,转换错误

var result = JsonConvert.DeserializeObject<T>(parsed["result"].ToString(),
                    new JsonSerializerSettings
                    {
                        /*
                         * Because Aliexpress Api logic is very weird - 
                         * it could return "-" for totalResults field, 
                         * Which is Integer by documentation and common sense
                        */
                        Error = HandleDeserializationError
                    });

 protected void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
        {
            var currentError = errorArgs.ErrorContext.Error.Message;
            errorArgs.ErrorContext.Handled = true;
        }

转换后的代码给出了

Dim result = JsonConvert.DeserializeObject(Of T)(parsed("result").ToString(), New JsonSerializerSettings() With { _
    Key .[Error] = HandleDeserializationError _
})

Protected Sub HandleDeserializationError(sender As Object, errorArgs As ErrorEventArgs)
    Dim currentError = errorArgs.ErrorContext.[Error].Message
    errorArgs.ErrorContext.Handled = True
End Sub

ErrorEventArgs类定义为

  Public Class ErrorEventArgs
        Inherits EventArgs

        Public Sub New(currentObject As Object, errorContext As ErrorContext)

        Public ReadOnly Property CurrentObject As Object
        Public ReadOnly Property ErrorContext As ErrorContext
    End Class

基本上我有转换过程:

Protected Sub HandleDeserializationError(sender As Object, errorArgs As ErrorEventArgs)
            Dim currentError As String = errorArgs.ErrorContext.[Error].Message
            errorArgs.ErrorContext.Handled = True
        End Sub

我需要将其指定为事件处理程序

这样的东西
 Dim s As JsonSerializerSettings = New JsonSerializerSettings()
                s.Error = HandleDeserializationError()
                Dim result = JsonConvert.DeserializeObject(Of T)(parsed("result").ToString(), s)

1 个答案:

答案 0 :(得分:2)

与生成的代码非常相似,但有一些更改。我不知道生成器使用Key执行了什么操作,并且我认为在此上下文中将Error关键字括起来是不必要的。

Dim Result = JsonConvert.DeserializeObject(OF T)(parsed("result").ToString(), _
                New JsonSerializerSettings With { .Error = AddressOf HandleDeserializationError} )


Protected Sub HandleDeserializationError(sender As Object, ErrorArgs As EventArgs)
    'The first line in this method didn't do anything
    errorArgs.ErrorContext.Handled = True
End Sub