我正在对项目进行一些代码分析并实施有意义的建议。一个建议是执行以下操作:
CA2000:Microsoft.Reliability:在方法“Service.ParseConfigurationFile()”中,在对对象“new SecureString()”的所有引用超出范围之前调用 System.IDisposable.Dispose
违规行如下:
Password = me.Password.Aggregate(new SecureString(), (secureString, c) => { secureString.AppendChar(c); return secureString; })
关于如何正确执行此操作的任何想法?我已经用下面的行替换了上面的内容,但我认为它不正确,因为它仍然会导致代码分析消息出现:
Password = me.Password.Aggregate(new SecureString(), (secureString, c) => { using (secureString) {secureString.AppendChar(c); return secureString;} })
编辑:根据@Jon 下面的评论 objectInstance 是一个名为 MailboxElement(我)的自定义类的实例。它通过配置文件中的多个自定义部分,如下所示:
foreach (MailboxElement me in mailboxesSection.Mailboxes)
{
MailboxInformation mailboxInformation = new MailboxInformation
{
ExchangeServerWebServiceUrl = me.ExchangeServerWebServiceUrl,
MailboxFriendlyName = me.FriendlyName,
UserName = me.UserName,
Password = me.Password.Aggregate(new SecureString(), (secureString, c) => { secureString.AppendChar(c); return secureString; }),
MailboxToAccess = me.MailboxToAccess
};
// Do stuff with mailboxInformation here
}
MailboxElement
是一个密封类,它实现了具有上述所有属性的ConfigurationElement 。
MailboxInformation
定义如下:
public class MailboxInformation
{
public string MailboxFriendlyName { get; set; }
public string UserName { get; set; }
public SecureString Password { get; set; }
public string ExchangeServerWebServiceUrl { get; set; }
public string MailboxToAccess { get; set; }
public string InboxFolderId { get; set; }
public string SentItemsFolderId { get; set; }
public bool MailboxSettingsDiscovered { get; set; }
}
我希望这能让事情更清楚......