【问题标题】:Getting NotImplementedException on foreach [closed]在 foreach 上获取 NotImplementedException [关闭]
【发布时间】:2013-12-24 11:20:50
【问题描述】:

试图从服务中获取数据并将其绑定到网格

我在 localitem 中获取数据,错误发生在“foreach”行

任何帮助将不胜感激

private IEnumerable<PersonalIDCheckerMvCKendo.Models.PersonInfo> Getlocalinfo(string personalNO)
{
    needsUpdate = false;

    using (PersonalInfoServiceClient serviceclient = new PersonalInfoServiceClient())
    {
        List<PersonalIDCheckerMvCKendo.Models.PersonInfo> personInfo = new List<PersonalIDCheckerMvCKendo.Models.PersonInfo>();

        try
        {
            IEnumerable localItem = serviceclient.GetLocalInfoForPerson(personalNO);

            if (localItem != null)
            {
                foreach (PersonalIDCheckerMvCKendo.Models.PersonInfo dalitem in localItem)
                {
                    personInfo.Add(new PersonalIDCheckerMvCKendo.Models.PersonInfo
                    {
                        DocumentSerie = dalitem.DocumentSerie,
                        DocumentNumber = dalitem.DocumentNumber,
                        DocumentType = dalitem.DocumentType,
                        DocumentIssuer = dalitem.DocumentIssuer,
                        DocumentValidDate = dalitem.DocumentValidDate
                    });                        
                }
            }
            return personInfo.ToArray();
        }
        catch
        {
            throw;
        }
        finally
        {
            serviceclient.Close();
        }
    }
}

编辑:

GetLocalInfoForPerson 是 PersonalInformation 类型

public partial class PersonalInformation : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged , System.Collections.IEnumerable 

【问题讨论】:

  • 请显示完整的堆栈跟踪。 (不清楚为什么你有一个毫无意义的catch 块,或者你的问题中有这么多空格......我会修复后者,但请在未来更加注意问题的可读性。)跨度>
  • 流行语完全是多余的,因此毫无意义。
  • 我打算改变catch块,需要先让方法工作
  • 问题可能出在 localItem 对象中,并且您没有显示创建它的代码。 IEnumerable 是一个接口,我猜想某些 localItem 类有一个存根,用于枚举引发异常的函数之一。如果您需要帮助,请在此问题中添加更多代码。

标签: c# asp.net-mvc exception notimplementedexception


【解决方案1】:

GetLocalInfoForPerson 返回的具体类型是什么?它是否正确实施IEnumerable

根据您的代码示例,很难说可能出了什么问题,因为我们不知道所涉及的类型。但是,您将localItem 键入为IEnumerable,如果这是自定义实现,则可能有人忘记实现非泛型GetEnumerator 方法。

如果将localItem 的声明更改为:

IEnumerable<PersonalIDCheckerMvCKendo.Models.PersonInfo> localItem = serviceclient.GetLocalInfoForPerson(personalNO);

这意味着IEnumerable.GetEnumerator 未实现(即抛出NotImplementedException),而IEnumerable&lt;T&gt;.GetEnumerator 实际上已实现。也可能是两种方法都没有实现。在代码中查找从 GetLocalInfoForPerson 返回的类型。

【讨论】:

  • 我在更改 localItem 的声明时收到此错误 - Error 1 Cannot implicitly convert type 'PersonalIDCheckerMvCKendo.PersonalInfoService.PersonalInformation' to 'System.Collections.Generic.IEnumerable&lt;PersonalIDCheckerMvCKendo.Models.PersonInfo&gt;'. An explicit conversion exists (are you missing a cast?) GetLocalInfoForPerson 的类型为 PersonalInformation 'public partial class PersonalInformation : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged , System.Collections.IEnumerable'
  • 那么 PersonalInformation 没有正确实现 IEnumrable。看看代码,GetEnumerator 方法在那个类上看起来如何?
  • public System.Collections.IEnumerator GetEnumerator() { throw new NotImplementedException(); } 怎么改?
  • @Nathaniel 编写实现方法,想想PersonalInformation应该如何枚举。
猜你喜欢
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
相关资源
最近更新 更多