SA1401 字段必须声明为私有访问.使用属性公开字段 [英] SA1401 Fields must be declared with private access. Use properties to expose fields

查看:26
本文介绍了SA1401 字段必须声明为私有访问.使用属性公开字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临以下 StyleCop 违规行为.我该如何解决这个问题?

I facing the following StyleCop violation. How can I fix this?

警告 SA1401:CSharp.Maintainability:必须使用私有访问声明字段.使用属性来公开字段.监控 GitLab 项目状态

Warning SA1401 : CSharp.Maintainability : Fields must be declared with private access. Use properties to expose fields. MonitoringGitLabProjectStatus

我的代码是

    public class EmailConfig
    {

        public EmailConfig()
        {
            this.AmazonClient = new AmazonSimpleEmailServiceClient(this.amazonUserName, this.amazonPassword);
        }
        protected MailMessage mailMessage = new MailMessage(); //Fields must be declared with private access. Use properties to expose fields
        protected RawMessage rawMessage = new RawMessage();  //Fields must be declared with private access. Use properties to expose fields
        protected SendRawEmailRequest request = new SendRawEmailRequest(); //Fields must be declared with private access. Use properties to expose fields
        protected List<string> mailNotifications = new List<string>(); //Fields must be declared with private access. Use properties to expose fields
        protected List<string> additionalNotifications = new List<string>(); //Fields must be declared with private access. Use properties to expose fields
        protected List<string> additionalNotificationsinBCC = new List<string>(); //Fields must be declared with private access. Use properties to expose fields
        private string amazonUserName = "user name";
        private string amazonPassword = "Password";
        public AmazonSimpleEmailServiceClient AmazonClient { get; protected set; }
    }
    public class EmailSenderThread : EmailConfig
    {
        private Thread msgThread;
        public EmailSenderThread(List<string> emailAddresses, List<string> ccemailaddress, List<string> bccemailaddress, string from, string subject, string body, string attachmentFileName)
            : base()
        {
            try
            {
                this.msgThread = new Thread(new ThreadStart(this.MailSender));
                mailMessage.From = new MailAddress(string.IsNullOrEmpty(from) ? "gitlabteam@syncfusion.com" : from);
                if (emailAddresses != null)
                {
                    var tomails = emailAddresses;
                    foreach (string tomail in tomails)
                    {
                        if (!string.IsNullOrEmpty(tomail))
                        {
                            mailMessage.To.Add(new MailAddress(tomail));
                            mailNotifications.Add(tomail);
                        }
                    }                            
                }

                if (ccemailaddress != null)
                {
                    var ccemails = ccemailaddress;
                    foreach (string ccmail in ccemails)
                    {
                        if (!string.IsNullOrEmpty(ccmail))
                        {
                            mailMessage.CC.Add(new MailAddress(ccmail));
                            additionalNotifications.Add(ccmail);
                        }
                    }                            
                }

                if (bccemailaddress != null)
                {
                    var bccemails = bccemailaddress;
                    foreach (string bccmail in bccemails)
                    {
                        if (!string.IsNullOrEmpty(bccmail))
                        {
                            mailMessage.Bcc.Add(new MailAddress(bccmail));
                            additionalNotificationsinBCC.Add(bccmail);
                        }
                    }                           
                }

                mailMessage.Subject = subject;
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, Encoding.UTF8, "text/html");
                if (body != null)
                {
                    mailMessage.AlternateViews.Add(htmlView);
                }

                if (!string.IsNullOrEmpty(attachmentFileName))
                {
                    var attachment = new Attachment(attachmentFileName);
                    mailMessage.Attachments.Add(attachment);
                }

                MemoryStream memoryStream = ConvertMailMessage.ConvertMailMessageToMemoryStream(mailMessage);
                rawMessage.WithData(memoryStream);
                request.WithRawMessage(this.rawMessage);
                request.WithDestinations(this.mailNotifications);
                request.WithDestinations(this.additionalNotifications);
                request.WithDestinations(this.additionalNotificationsinBCC);
                request.WithSource(from);
                this.msgThread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in mail sending: {0}", ex);
            }
        }

我标记了我面临 StyleCop 违规的评论行.我不知道如何访问 公共类 EmailSenderThread : EmailConfig 中的 Private 字段.如果我更改 Private 而不是 protected 意味着我在 public class EmailSenderThread : EmailConfig 中遇到异常.我尝试通过为 EmailConfig 创建对象来修复错误.但是没用过.

I marked the comment line where I faceing the StyleCop violation. I do not know how to access the Private feild in the public class EmailSenderThread : EmailConfig. If I change the Private instead of protected means I got exception in public class EmailSenderThread : EmailConfig. I tried by fix the error by creating the object for the EmailConfig. But not used.

推荐答案

您应该使用 C# 属性.

You should use C# properties.

例如:

public class EmailConfig
{
    public EmailConfig()
    {
        this.AmazonClient = new AmazonSimpleEmailServiceClient(this.amazonUserName, this.amazonPassword);
    }
    protected MailMessage MailMessage { get; set; } = new MailMessage(); //Fields must be declared with private access. Use properties to expose fields
    protected RawMessage RawMessage { get; set; } = new RawMessage();  //Fields must be declared with private access. Use properties to expose fields
    protected SendRawEmailRequest Request { get; set; } = new SendRawEmailRequest(); //Fields must be declared with private access. Use properties to expose fields
    protected List<string> MailNotifications { get; set; } = new List<string>(); //Fields must be declared with private access. Use properties to expose fields
    protected List<string> AdditionalNotifications { get; set; } = new List<string>(); //Fields must be declared with private access. Use properties to expose fields
    protected List<string> AdditionalNotificationsinBCC { get; set; } = new List<string>(); //Fields must be declared with private access. Use properties to expose fields
    private string amazonUserName = "user name";
    private string amazonPassword = "Password";
    public AmazonSimpleEmailServiceClient AmazonClient { get; protected set; }
}

这篇关于SA1401 字段必须声明为私有访问.使用属性公开字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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