Parameter Count mismatch with an extension method

  • Thread starter Thread starter rothco
  • Start date Start date
R

rothco

Guest
Hello

I have the following extension method

Imports System.Runtime.CompilerServices
Imports System.Net.Mail
Imports System.IO
Imports System.Reflection

Module SMTPExt
<Extension()>
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")

Using _fileStream As FileStream = New FileStream(FileName, FileMode.Create)
Dim _mailWriterContructor As ConstructorInfo = _mailWriterType.GetConstructor(BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing)
Dim _mailWriter As Object = _mailWriterContructor.Invoke(New Object() {_fileStream})
Dim _sendMethod As MethodInfo = GetType(MailMessage).GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic)
_sendMethod.Invoke(Message, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {_mailWriter, True}, Nothing)
Dim _closeMethod As MethodInfo = _mailWriter.[GetType]().GetMethod("Close", BindingFlags.Instance Or BindingFlags.NonPublic)
_closeMethod.Invoke(_mailWriter, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {}, Nothing)
End Using
End Sub
End Module


When I call this method with this code I get "Parameter Count Mismatch" - why? (I am just showing the relevant code)

Imports AATDB.SMTPExt

Dim reportEmail As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(SPUsername, ToMailAddress, Subject, Htmlbody)

Dim tempEmlPath As String = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() & ".eml")
reportEmail.save(tempEmlPath)
Process.Start(tempEmlPath)





Roy T

Continue reading...
 
Back
Top