【问题标题】:GetConstructor() method returning null value in .Net 6 but the same returning values in .Net core 3.1GetConstructor() 方法在 .Net 6 中返回空值,但在 .Net 核心 3.1 中返回相同的值
【发布时间】:2022-08-18 15:02:22
【问题描述】:

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

错误信息:你调用的对象是空的。

它的发生是因为 GetConstructor() 方法返回空值

请找到代码的sn-p:

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);
            }
  • 这表明您正在使用的类型的实现已更改,并且不再有符合指定条件的构造函数。您可以在两个版本中调用 GetConstructors 并查看有什么区别,然后更改 .NET 6 的标准以获得适当的可用构造函数。 MailWriter 类被声明为 internal 并且他们对更改此类类型的接口不太害羞,因为在进行更改的团队之外没有人应该使用它们。

标签: c# asp.net-core asp.net-core-mvc .net-6.0


【解决方案1】:

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

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.
        {
        }

它在this GitHub pull 中发生了变化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2021-10-15
    • 2021-01-07
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多