Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用IDispose错误实现IEnumerator/IEnumerable_C#_.net_Generics - Fatal编程技术网

C# 使用IDispose错误实现IEnumerator/IEnumerable

C# 使用IDispose错误实现IEnumerator/IEnumerable,c#,.net,generics,C#,.net,Generics,我一直在尝试在IEnumorator中构建,以允许我使用foreach语句将从JSON格式字符串提取的大量行快速插入到这些嵌套对象中。Applines类专门由字符串Get/Set语句组成 出于某种原因,我得到了指向类标题的三个错误。我不能理解 a) 为什么以及如何实现IDisposable b) 为什么编译器不同意我的返回类型 “AppAnnieImport”未实现接口成员“System.Collections.IEnumerable.GetEnumerator()”AppAnnieImport

我一直在尝试在IEnumorator中构建,以允许我使用foreach语句将从JSON格式字符串提取的大量行快速插入到这些嵌套对象中。Applines类专门由字符串Get/Set语句组成

出于某种原因,我得到了指向类标题的三个错误。我不能理解 a) 为什么以及如何实现IDisposable b) 为什么编译器不同意我的返回类型

“AppAnnieImport”未实现接口成员“System.Collections.IEnumerable.GetEnumerator()”AppAnnieImport.GetEnumerator()无法实现“System.Collections.IEnumerable.GetEnumerator()”,因为它没有匹配的返回类型“System.Collections.IEnumerator”

“AppAnnieImport”未实现接口成员“System.IDisposable.Dispose()”

“AppAnnieImport”未实现接口成员“System.Collections.Generic.IEnumerator.Current”AppAnnieImport.Current无法实现System.Collections.Generic.IEnumerator.Current,因为它没有匹配的返回类型“AppLines”

公共类AppAnnieImport:IEnumerator,IEnumerable
{
公共整数代码{get;set;}
公共日期时间结束日期{get;set;}
公共字符串垂直{get;set;}
公共字符串粒度{get;set;}
公共字符串设备{get;set;}
公共列表应用程序{get;set;}
私人职位;
//IEnumerator和IEnumerable需要这些方法。
公共IEnumerator GetEnumerator()
{
返回(IEnumerator)这个;
}
//电子计算器
公共图书馆
{
位置++;
返回(位置<应用计数);
}
//数不清
公共无效重置()
{position=0;}
//数不清
公共对象流
{
获取{return(AppLine.ToArray())[position];}
}
}
,因此您也必须实现它,并提供一个
Dispose
方法

IEnumerator
继承自
IEnumerator
,因此您也必须实现它,并提供一个
GetEnumerator()
函数返回一个
IEnumerator

,所以您必须实现一个


我猜混淆是因为泛型
IEnumerable
IEnumerator
继承自非泛型版本(
IEnumerable
IEnumerator
),要求实现具有相同名称但不同返回值类型的方法;而
IEnumerator
继承自
IDisposable
,而
IEnumerator
不继承


所以,最后,你的班级应该看起来像

public class AppAnnieImport : IEnumerator<AppLines>, IEnumerable<AppLines>
{
    public int code { get; set; }
    public DateTime end_date { get; set; }
    public string vertical { get; set; }
    public string granularity { get; set; }
    public string device { get; set; }
    public List<AppLines> AppLine { get; set; }
    private int position;

    //IEnumerator and IEnumerable require these methods.
    public IEnumerator<AppLines> GetEnumerator()
    {
        return (IEnumerator<AppLines>)this;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return (IEnumerator<AppLines>)this;
    }

    //IEnumerator
    public bool MoveNext()
    {
        position++;
        return (position < AppLine.Count);
    }

    //IEnumerable
    public void Reset()
    { position = 0; }

    //IEnumerable
    object IEnumerator.Current
    {
        get { return (AppLine.ToArray())[position] ; }
    }

    public AppLines Current
    {
        get { return (AppLine.ToArray())[position] ; }
    }

    public void Dispose()
    {
        // do something or not
    }
}
公共类AppAnnieImport:IEnumerator,IEnumerable
{
公共整数代码{get;set;}
公共日期时间结束日期{get;set;}
公共字符串垂直{get;set;}
公共字符串粒度{get;set;}
公共字符串设备{get;set;}
公共列表应用程序{get;set;}
私人职位;
//IEnumerator和IEnumerable需要这些方法。
公共IEnumerator GetEnumerator()
{
返回(IEnumerator)这个;
}
IEnumerator IEnumerable.GetEnumerator()
{
返回(IEnumerator)这个;
}
//电子计算器
公共图书馆
{
位置++;
返回(位置<应用计数);
}
//数不清
公共无效重置()
{position=0;}
//数不清
对象IEnumerator.Current
{
获取{return(AppLine.ToArray())[position];}
}
公共应用程序当前
{
获取{return(AppLine.ToArray())[position];}
}
公共空间处置()
{
//做点什么还是不做
}
}

使用显式接口实现(您可能还希望消除代码重复)

首先在自定义逻辑中重用内部列表的方法和层,然后:

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class AppLines { }
    public class AppAnnieImport : IEnumerator<AppLines>, IEnumerable<AppLines>
    {
        public int code { get; set; }
        public DateTime end_date { get; set; }
        public string vertical { get; set; }
        public string granularity { get; set; }
        public string device { get; set; }
        public List<AppLines> appLines { get; set; }
        private int position;

        AppLines IEnumerator<AppLines>.Current
        {
            get { return ((IEnumerator<AppLines>)this.appLines).Current; }
        }

        public void Dispose()
        {
            ((IEnumerator<AppLines>)this.appLines).Dispose();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.appLines.GetEnumerator();
        }

        object System.Collections.IEnumerator.Current
        {
            get { return ((System.Collections.IEnumerator)this.appLines).Current; }
        }

        public bool MoveNext()
        {
            return ((System.Collections.IEnumerator)this.appLines).MoveNext();
        }

        public void Reset()
        {
            ((System.Collections.IEnumerator)this.appLines).Reset();
        }

        IEnumerator<AppLines> IEnumerable<AppLines>.GetEnumerator()
        {
            return ((IEnumerable<AppLines>)this.appLines).GetEnumerator();
        }
    }
}
使用系统;
使用System.Collections.Generic;
命名空间控制台应用程序1
{
公共类应用程序{}
公共类AppAnnieImport:IEnumerator,IEnumerable
{
公共整数代码{get;set;}
公共日期时间结束日期{get;set;}
公共字符串垂直{get;set;}
公共字符串粒度{get;set;}
公共字符串设备{get;set;}
公共列表应用程序{get;set;}
私人职位;
AppLines IEnumerator.Current
{
获取{return((IEnumerator)this.appLines.Current;}
}
公共空间处置()
{
((IEnumerator)this.appLines).Dispose();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回此.appLines.GetEnumerator();
}
对象System.Collections.IEnumerator.Current
{
获取{return((System.Collections.IEnumerator)this.appLines.Current;}
}
公共图书馆
{
return((System.Collections.IEnumerator)this.appLines.MoveNext();
}
公共无效重置()
{
((System.Collections.IEnumerator)this.appLines.Reset();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回((IEnumerable)this.appLines).GetEnumerator();
}
}
}

将光标放在
IEnumerator
上,然后按Ctrl+。并选择实现接口。基本上,您需要实现通用和非通用版本的
GetEnumerator
。编译器对问题的解释中没有添加任何内容。我不会使类同时具有IEnumerable和IEnumerator,实现IEnumerable,如果您需要自定义IEnumerator,让它成为一个单独的类。我同意@LasseV.Karlsen的观点,至少在公共类中是这样(有时可以方便地在私有类或内部类中实现,因为在私有类或内部类中,混淆的可能性要小得多)。thaks,这是完美的
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class AppLines { }
    public class AppAnnieImport : IEnumerator<AppLines>, IEnumerable<AppLines>
    {
        public int code { get; set; }
        public DateTime end_date { get; set; }
        public string vertical { get; set; }
        public string granularity { get; set; }
        public string device { get; set; }
        public List<AppLines> appLines { get; set; }
        private int position;

        AppLines IEnumerator<AppLines>.Current
        {
            get { return ((IEnumerator<AppLines>)this.appLines).Current; }
        }

        public void Dispose()
        {
            ((IEnumerator<AppLines>)this.appLines).Dispose();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.appLines.GetEnumerator();
        }

        object System.Collections.IEnumerator.Current
        {
            get { return ((System.Collections.IEnumerator)this.appLines).Current; }
        }

        public bool MoveNext()
        {
            return ((System.Collections.IEnumerator)this.appLines).MoveNext();
        }

        public void Reset()
        {
            ((System.Collections.IEnumerator)this.appLines).Reset();
        }

        IEnumerator<AppLines> IEnumerable<AppLines>.GetEnumerator()
        {
            return ((IEnumerable<AppLines>)this.appLines).GetEnumerator();
        }
    }
}