vb.net Visual Basic 重载解析失败,因为无法调用可访问的“New”,错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13011234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:00:56  来源:igfitidea点击:

Visual Basic Overload resolution failed because no accessible 'New' can be called, error

vb.netemail

提问by UrsulRosu

I have this code from this question:

我有这个问题的代码:

Generate a PDF file as system.net.mail.attachment using Memory Stream

使用内存流生成一个 PDF 文件作为 system.net.mail.attachment

to help me create an email attachment in memory.

帮助我在内存中创建电子邮件附件。

Imports System.IO
Imports System.Net.Mail
Imports System.Text.ASCIIEncoding
Imports System.net.Mime

Public Sub SendMail(ByVal att As String, Optional ByVal filename As String _
    = "Attachment.csv")

    Dim sendMail As New SmtpClient
    Dim mail As New MailMessage

    Using MemoryStream = New MemoryStream

        If att.Length <> 0 Then

            Dim data As Byte() = ASCII.GetBytes(att)

            MemoryStream.Write(data, 0, data.Length)
            MemoryStream.Seek(0, SeekOrigin.Begin)
            MemoryStream.Position = 0

            Dim content As New Net.Mime.ContentType()

            content.MediaType = MediaTypeNames.Application.Octet
            content.Name = filename

            Dim Attach As Attachment
            Attach = New Attachment(MemoryStream, content)
            mail.Attachments.Add(Attach)
        End If

        sendMail.DeliveryMethod = SmtpDeliveryMethod.Network
        sendMail.Host = "SERVER"
        sendMail.UseDefaultCredentials = False

        sendMail.Credentials = New System.Net.NetworkCredential("UN", "PW")
        sendMail.Send(mail)
    End Using
End Sub

I receive this error :

我收到此错误:

Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:

'Public Sub New(contentStream As System.IO.Stream, contentType As System.Net.Mime.ContentType)': Argument matching parameter 'contentStream' narrows from 'Object' to 'System.IO.Stream'.

'Public Sub New(fileName As String, contentType As System.Net.Mime.ContentType)': Argument matching parameter 'fileName' narrows from 'Object' to 'String'.

重载解析失败,因为在没有缩小转换的情况下无法调用可访问的“新”:

'Public Sub New(contentStream As System.IO.Stream, contentType As System.Net.Mime.ContentType)':参数匹配参数'contentStream'从'Object'缩小到'System.IO.Stream'。

'Public Sub New(fileName As String, contentType As System.Net.Mime.ContentType)':参数匹配参数'fileName'从'Object'缩小到'String'。

Dim Attach As Attachment
Attach = New Attachment(MemoryStream, content)

at this line.

在这一行。

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Hyman Gajanan

or use this

或使用这个

Using MemoryStream As MemoryStream = new MemoryStream()

回答by Teppic

In your Using statement you are initialising the variable named MemoryStream as an Object rather than as a MemoryStream.

在您的 Using 语句中,您将名为 MemoryStream 的变量初始化为 Object 而不是 MemoryStream。

Try changing your Using statement from

尝试将您的 Using 语句从

Using MemoryStream = New MemoryStream

to

Using MemoryStream As New MemoryStream