GetConstructor() 方法在 .Net 6 中返回 null 值,但在 .Net core 3.1 中返回相同的值

Gop*_*ani 2 c# asp.net-core-mvc asp.net-core .net-6.0

我正在尝试将邮件消息保存在文件中。所以我使用下面的代码将邮件消息保存在文件中。相同的代码在 .Net core 3.1 中工作,但在 .Net 6 上抛出错误。

错误信息: 未将对象引用设置为对象的实例。

发生这种情况是因为 GetConstructor() 方法返回 null 值

请找到代码片段:

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);
            }
Run Code Online (Sandbox Code Playgroud)

Cha*_*ace 7

这个内部类的实现已经改变,这不应该让你感到惊讶,因为它是内部的并且没有记录,因此随时可能发生变化(这包括次要版本,而不仅仅是主要版本更改)。

以前的代码有

        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.
        {
        }
Run Code Online (Sandbox Code Playgroud)

现在它有

        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.
        {
        }
Run Code Online (Sandbox Code Playgroud)

在此 GitHub 拉取中发生了变化