C#静态类型不能用作参数 [英] C# Static types cannot be used as parameters

查看:117
本文介绍了C#静态类型不能用作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void SendEmail(String from, String To, String Subject, String HTML, String AttachmentPath = null, String AttachmentName = null, MediaTypeNames AttachmentType = null)
{
    ....

    // Add an attachment if required
    if (AttachmentPath != null)
    {
        var ct = new ContentType(MediaTypeNames.Text.Plain);
        using (var a = new Attachment(AttachmentPath, ct)
                    {
                        Name = AttachmentName,
                        NameEncoding = Encoding.UTF8,
                        TransferEncoding = TransferEncoding.Base64
                    })
        {
            mailMessage.Attachments.Add(a);
        }
    }

    ....
}

如您所见, MediaTypeNames AttachmentType 会引发错误:

As you can see the MediaTypeNames AttachmentType throws the error:

'System.Net.Mime.MediaTypeNames': static types cannot be used as parameters

处理此问题的最佳方法是什么?

What is the best way to deal with this?

推荐答案

您不能将静态类型作为参数传递给方法,因为这样一来就必须实例化它,并且不能创建a的实例. static 类.

You can't pass a static type to a method as a parameter because then it would have to be instantiated, and you can't create an instance of a static class.

这篇关于C#静态类型不能用作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆