【发布时间】:2017-05-31 20:16:49
【问题描述】:
我有一堂课如下:
using System;
using System.Collections;
using System.Collections.Generic;
namespace DataBinding.Schemas.Scenario.ViewModels
{
public class InternationalRowViewDetailsModel: IEnumerable
{
public InternationalRowViewDetailsModel()
{
Country = new System.Collections.Generic.Dictionary<object, object>();
RateEUR = new System.Collections.Generic.Dictionary<object, object>();
RateGBP = new System.Collections.Generic.Dictionary<object, object>();
RateUSD = new System.Collections.Generic.Dictionary<object, object>();
}
public Dictionary<object, object> Country { get; set; }
public Dictionary<object, object> RateEUR { get; set; }
public Dictionary<object, object> RateGBP { get; set; }
public Dictionary<object, object> RateUSD { get; set; }
public IEnumerator GetEnumerator() { return (IEnumerator)this; }
}
}
现在我正在尝试遍历 InternationalRowViewDetailsModel 的值,如下所示:
foreach (InternationalRowViewDetailsModel currentRow in viewModel)
{
Row newRow = FillInternationalNumberRow(itemRow, currentRow);
tableNode.Rows.Add(newRow);
}
我想使用 foreach 并将每个当前行发送到 FillInternationalNumberRow 方法,该方法填充表格的列。
private static Row FillInternationalNumberRow(Row rowTemplate, InternationalRowViewDetailsModel internationalViewModel)
{
Row newRow = rowTemplate.Clone(true) as Row;
int currentCell = 0;
if (newRow != null)
{
newRow.Cells[currentCell++].FirstParagraph.Runs[0].Text = internationalViewModel.Country.ToString();
newRow.Cells[currentCell++].FirstParagraph.Runs[0].Text = internationalViewModel.RateEUR.ToString();
newRow.Cells[currentCell++].FirstParagraph.Runs[0].Text = internationalViewModel.RateGBP.ToString();
newRow.Cells[currentCell++].FirstParagraph.Runs[0].Text = internationalViewModel.RateUSD.ToString();
}
return newRow;
}
但我收到以下错误: 无法将类型为“DataBinding.Schemas.Scenario.ViewModels.InternationalRowViewDetailsModel”的对象转换为类型“System.Collections.IEnumerator”
【问题讨论】:
-
返回 (IEnumerator)this;您从未定义过枚举器。而是您试图将其转换为 IEnumerator
-
您似乎想在 foreach 循环中使用 InternationalRowViewDetailsModel 类,但从代码中不清楚您希望从 foreach 循环中得到什么。
-
@Scott Morken 我正在尝试使用 foreach 循环并遍历 InternationalRowViewDetailsModel 的每一行。
标签: c# ienumerable ienumerator