SA1401 Fields must be declared with private access. Use properties to expose fields

試著忘記壹切 提交于 2020-12-26 20:09:12

问题


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

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

My code is

    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);
            }
        }

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.


回答1:


You should use C# properties.

For example:

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; }
}


来源:https://stackoverflow.com/questions/43775923/sa1401-fields-must-be-declared-with-private-access-use-properties-to-expose-fie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!