2010-10-15 105 views
1

我已经定义了一个对象作为无法赋值给foreach循环中对象的属性?

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值时,它不是数据参数,而是变量。但我该如何解决这个问题?我是否对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 to XML,但我可以弄清楚。

+0

老实说,我想通过Linq做到这一点,但我似乎无法找到一种方法来分配值。 – BryanGrimes 2010-10-15 16:51:20

+0

嗯......有时候你需要一个普通的旧的for循环而不是foreach,但我没有一个好的答案。另外,什么是实际类型?你能暂时停止使用'var'吗?你需要使用什么类型的? http://stackoverflow.com/questions/2196638/c-cannot-assign-to-foreach-iteration-variable – 2010-10-15 16:57:06

回答

1

是收件人的结构?也许将它改为一个类。

+0

这是一个公共课 – BryanGrimes 2010-10-15 17:11:13

1

。为了以来先后成立,这样做:

  • 创建收件人的新列表
  • 的foreach原始收件人构建新的,并将其插入新的列表
  • 分配新的列表。为

这应该工作。