【问题标题】:Unable to cast object of type 'InternationalRowViewDetailsModel' to type 'System.Collections.IEnumerator'无法将“InternationalRowViewDetailsModel”类型的对象转换为“System.Collections.IEnumerator”类型
【发布时间】: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


【解决方案1】:

您没有正确实现接口。这是一个实现IEnumerable的类型:

public class A : IEnumerable {
   private List<string> items = new List<string>();

   public IEnumerator GetEnumerator() {
      return this.items.GetEnumerator();
   }
}

在上面的代码中,我只是返回了内部列表的IEnumerator

请记住,当您使用IEnumerable 时,它将产生Object 类型的项目。所以即使在上面我有一个List&lt;string&gt;,当遍历它时,它也会返回类型Object。尽量使用通用的IEnumerable&lt;T&gt;,以避免装箱和拆箱。

【讨论】:

    【解决方案2】:

    你的类实现了IEnumerable

    public class InternationalRowViewDetailsModel: IEnumerable
    

    但你试图将其转换为 IEnumerator:

    public IEnumerator GetEnumerator() { return (IEnumerator)this; }
    

    您要迭代的对象也必须实现IEnumerator,以便代码知道如何遍历您的集合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-06
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多