Home > database >  GetConstructor() method returning null value in .Net 6 but the same returning values in .Net core 3.
GetConstructor() method returning null value in .Net 6 but the same returning values in .Net core 3.

Time:08-19

I am trying to save the mailmessage in file. So i have used below code to save the mailmessage in file. the same code working in .Net core 3.1 but it's throwing error on .Net 6.

Error Info: Object reference not set to an instance of an object.

Its happening because for GetConstructor() method returning null value

Please find the snippet of code:

Assembly assembly = typeof(SmtpClient).Assembly;
            Type _mailWriterType =
              assembly.GetType("System.Net.Mail.MailWriter");

            using (FileStream _fileStream =
                   new FileStream(FileName, FileMode.Create))
            {
                // Get reflection info for MailWriter contructor
                ConstructorInfo _mailWriterContructor =
                    _mailWriterType.GetConstructor(
                        BindingFlags.Instance | BindingFlags.NonPublic,
                        null,
                        CallingConventions.HasThis,
                        new Type[] { typeof(Stream) },
                        null);

                // Construct MailWriter object with our FileStream
                object _mailWriter =
                  _mailWriterContructor.Invoke(new object[] { _fileStream });

                // Get reflection info for Send() method on MailMessage
                MethodInfo _sendMethod =
                    typeof(MailMessage).GetMethod(
                        "Send",
                        BindingFlags.Instance | BindingFlags.NonPublic);

                // Call method passing in MailWriter
                _sendMethod.Invoke(
                    Message,
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new object[] { _mailWriter, true, true },
                    null);

                // Finally get reflection info for Close() method on our MailWriter
                MethodInfo _closeMethod =
                    _mailWriter.GetType().GetMethod(
                        "Close",
                        BindingFlags.Instance | BindingFlags.NonPublic);

                // Call close method
                _closeMethod.Invoke(
                    _mailWriter,
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new object[] { },
                    null);
            }

CodePudding user response:

The implementation of this internal class has changed, which shouldn't surprise you as it's internal and not documented, and therefore subject to change at any time (this includes minor builds, not just major version changes).

The code used to have

        internal MailWriter(Stream stream)
            : base(stream, true)
        // This is the only stream that should encoding leading dots on a line.
        // This way it is done message wide and only once.
        {
        }

Now it has

        internal MailWriter(Stream stream, bool encodeForTransport)
            : base(stream, encodeForTransport)
        // This is the only stream that should encoding leading dots on a line.
        // This way it is done message wide and only once.
        {
        }

It changed in this GitHub pull

  • Related