Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET邮件附件内联_C#_Asp.net - Fatal编程技术网

C# ASP.NET邮件附件内联

C# ASP.NET邮件附件内联,c#,asp.net,C#,Asp.net,我有一行创建附件的代码 email.Attachments.Add(new Attachment(new MemoryStream(filebytes), "QRCode.png")); 现在,我正在尝试将ContentDisposition.Inline属性应用于它……当以我的方式创建附件时,我将如何做到这一点?我想您需要一行解决方案,这是一种方法: email.Attachments.Add((new Func<Stream, Attachment>(stream =>

我有一行创建附件的代码

email.Attachments.Add(new Attachment(new MemoryStream(filebytes), "QRCode.png"));

现在,我正在尝试将ContentDisposition.Inline属性应用于它……当以我的方式创建附件时,我将如何做到这一点?

我想您需要一行解决方案,这是一种方法:

email.Attachments.Add((new Func<Stream, Attachment>(stream => { var attachment = new Attachment(stream, "QRCode.png"); attachment.ContentDisposition.Inline = true; return attachment; }).Invoke(new MemoryStream(fileBytes))));
email.Attachments.Add((new Func(stream=>{var attachment=new attachment(stream,“QRCode.png”);attachment.ContentDisposition.Inline=true;return attachment;}).Invoke(new MemoryStream(fileBytes));

然而,我强烈反对这种方法,因为它非常难阅读。

@darjan bogdan的解决方案是可行的,如果出于某种原因,它绝对需要是一行

但是,如果目标是只使用一行逻辑语句来添加附件,那么我只需要使用一个helper方法,即la:

Attachment data = BuildAttachment(new MemoryStream(filebytes), "QRCode.png"));
...

private Attachment BuildAttachment(MemoryStream stream, string name) {
    Attachment attachment = new Attachment(stream, name);
    attachment.ContentDisposition.Inline = true;

    return attachment;
}

这保留了我假设的for循环中的“单行”。

您需要一行解决方案吗?我同意您的观点,代码应该是可读的。