2017-02-10 33 views
0

_sendMethod.Invoke發生錯誤。 你知道如何解決這個問題嗎?有這個C#代碼。它似乎工作正常。但它不適用於vb.net。我需要爲此編寫VB.NET。System.Net.Mail.MailMessage擴展中的參數計數不匹配

這是關於例外的通知。

堆棧跟蹤:在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(對象OBJ,的BindingFlags invokeAttr,粘結劑粘結劑,對象[]參數,CultureInfo的培養物) 在System.Reflection.RuntimeMethodInfo.Invoke(對象OBJ,的BindingFlags invokeAttr,粘結劑粘結劑,Object []參數,CultureInfo culture) at PIPEmail.MailMessageExt.Save(MailMessage Message,String FileName)

消息:「參數計數不匹配」。

數據:{System.Collections.ListDictionaryInternal}

TragetSite:{System.Object的[] InvokeArgumentsCheck(System.Object的,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object的[],系統.Globalization.CultureInfo)}

Public Sub Save(ByVal Message As MailMessage, ByVal FileName As String) 
    Dim assembly As Assembly = GetType(SmtpClient).Assembly 
    Dim _mailWriterType As Type = assembly.[GetType]("System.Net.Mail.MailWriter") 

    Try 



     Using _fileStream As New FileStream(FileName, FileMode.Create) 
      ' Get reflection info for MailWriter contructor 
      Dim _mailWriterContructor As ConstructorInfo = _mailWriterType.GetConstructor(BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing) 

      ' Construct MailWriter object with our FileStream 
      Dim _mailWriter As Object = _mailWriterContructor.Invoke(New Object() {_fileStream}) 

      ' Get reflection info for Send() method on MailMessage 
      Dim _sendMethod As MethodInfo = GetType(MailMessage).GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic) 

      ' Call method passing in MailWriter 
      **_sendMethod.Invoke(Message, 
           BindingFlags.Instance Or BindingFlags.NonPublic, 
           Nothing, 
           New Object() {_mailWriter, True}, 
           Nothing 
           )** 

      '_sendMethod.Invoke(Message, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, 
      ' Finally get reflection info for Close() method on our MailWriter 
      Dim _closeMethod As MethodInfo = _mailWriter.[GetType]().GetMethod("Close", BindingFlags.Instance Or BindingFlags.NonPublic) 

      ' Call close method 
      _closeMethod.Invoke(_mailWriter, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {}, Nothing) 
     End Using 


    Catch ex As Exception 

    End Try 
End Sub 

前端模塊

回答

0

我加入 '' IN下面的代碼那麼它的工作原理。

_sendMethod.Invoke(消息, BindingFlags.Instance或者BindingFlags.NonPublic, 沒什麼, 新的對象(){_mailWriter,真,真}, 沒有 )