我已经实现了一个自定义链表,并且在实现IEnumerator<>时遇到了问题。具体来说,编译器告诉我The name "GetEnumerator" does not exist in the current context
。我觉得我正在实现它,就像我在许多stackoverflow帖子和教程中看到的那样,我缺少什么?
这是我的数据结构:
namespace TestReportCreator_v3
{
public class FindingsTable : IEnumerable<string>
{
private Node head, mark;
public class Node
{
public string description; //covers weakness, retinaDesc, nessusDesc
public string riskLevel; //covers impactLevel, retinaRisk, nessusRisk
public string boxName; //box name results apply to
public string scanner; //wassp, secscn, retina, nessus
public string controlNumber; //ia control number impacted, wassp and secscn only
public string fixAction; //comments, retinaFix, nessusSolu
public string auditID; //nessus plugin, retina AuditID, wassp/secscn test number
public Node next;
public Node(string auditID, string riskLevel, string boxName, string scanner, string controlNumber, string fixAction, string description, Node next)
{
this.description = description;
this.riskLevel = riskLevel;
this.boxName = boxName;
this.scanner = scanner;
this.controlNumber = controlNumber;
this.fixAction = fixAction;
this.auditID = auditID;
this.next = next;
}
}
...insert, delete, update, save methods...
public IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
var node = mark;
while (node != null)
{
yield return node.riskLevel;
node = node.next;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
IEnumerable<string>.GetEnumerator()
是一个显式接口实现,因此您需要在作为接口访问的实例上调用它。最简单的方法是投射this
:
return ((IEnumerable<string>)this).GetEnumerator();
请参阅如何在没有显式强制转换的情况下在内部调用显式接口实现方法?寻找替代方案。