我有一个对象定义为
public class EmailData
{
public string TemplateId { get; set;}
public IEnumerable<Recipient> To { get; set; }
public IEnumerable<Recipient> CC { get; set; }
public IEnumerable<Recipient> BCC { get; set; }
}
public class Recipient
{
public string Key { get; set; }
public string Type { get; set; }
public string Email { get; set; }
public string Id { get; set; }
}
我已经填充了一个构造函数,然后我需要完成填充收件人的值,简而言之,我会这样做(经过各种尝试):
public EmailData EmailObject;
public void BuildEmailData()
{
EmailDataBuilder builder = new EmailDataBuilder(EmailObject,CSGTemplateId);
EmailObject = builder.BuildObject();
}
public void PopulateEmailAddresses()
{
SetToValues();
}
private void SetToValues()
{
foreach (Recipient t in EmailObject.To)
{
var _user = RepoUser.GetUserById(t.Key);
t.Email = _user.Email;
t.Id = _user.Id;
}
}
所以你看到我有一个对象,我需要在其上填充更多的字段。现在,我得到foreach外观创建一个我正在使用的变量,所以当我分配Email值时,它不是数据arg,而是变量。但是我该如何解决这个问题呢?我错过了与foreach更简单的东西吗?
非常感谢,在我意识到作业无法正常工作之前,这是轰炸我的单元测试。
我工作的不喜欢的是直接使用收件人而不是IEnumerable:
public class EmailData
{
public string TemplateId { get; set;}
public RecipientCollection To { get; set; }
public RecipientCollection CC { get; set; }
public RecipientCollection BCC { get; set; }
}
public class RecipientCollection: IEnumerable<Recipient>
{
private readonly List<Recipient> variableList = new List<Recipient>();
public IEnumerator<Recipient> GetEnumerator()
{
return variableList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(Recipient v)
{
variableList.Add(v);
}
}
public EmailData BuildObject()
{
// get the template.
EmailObj.TemplateId = _templateRepo.GetCSGEmailTemplate(CSGTemplateId).TemplateId;
// populate the people
RecipientCollection _toCollection = new RecipientCollection();
RecipientCollection _ccCollection = new RecipientCollection();
RecipientCollection _bccCollection = new RecipientCollection();
foreach (var r in _recipRepo.GetRecipientByAction("To"))
{ _toCollection.Add(r); }
EmailObj.To = _toCollection;
foreach (var r in _recipRepo.GetRecipientByAction("CC"))
{ _ccCollection.Add(r); }
EmailObj.CC = _ccCollection;
foreach (var r in _recipRepo.GetRecipientByAction("BCC"))
{ _bccCollection.Add(r); }
EmailObj.BCC = _bccCollection;
return EmailObj;
}
public void BuildEmailData()
{
EmailDataBuilder builder = new EmailDataBuilder(EmailObject,CSGTemplateId);
EmailObject = builder.BuildObject();
}
public void PopulateEmailAddresses()
{
foreach (Recipient t in EmailObject.To)
{
var _user = RepoUser.GetUserById(t.Key);
t.Email = _user.Email;
t.Id = _user.Id;
}
}
这是不理想的,因为它打破了使用Linq到XML的另一个部分,但我可以想出来。
答案 0 :(得分:1)
收件人是结构吗?把它改成一个类。
答案 1 :(得分:1)
自。已设置,请执行:
这应该有效。