目前我已经像下面那样实现了它,但我不喜欢它因为GetEnumerator中的代码重复,我该如何删除重复...
public class EodSettlementRequestStubData : IEnumerable<SettlementRequest>
{
public SettlementRequest UnapprovedFlows;
public SettlementRequest UnapprovedRecovery;
public SettlementRequest UnverifiedFlows;
public SettlementRequest UnverifiedRecovery;
IEnumerator<SettlementRequest> IEnumerable<SettlementRequest>.GetEnumerator()
{
yield return UnapprovedFlows;
yield return UnapprovedRecovery;
yield return UnverifiedFlows;
yield return UnverifiedRecovery;
}
public IEnumerator GetEnumerator()
{
yield return UnapprovedFlows;
yield return UnapprovedRecovery;
yield return UnverifiedFlows;
yield return UnverifiedRecovery;
}
}
我希望能够单独访问它,以及IEnumerable来做一些linq的东西......
我可以创建一个列表以避免代码重复,但我觉得应该有更好的方法来处理这个......
答案 0 :(得分:4)
让非泛型版本调用泛型版本(将explictit接口版本设置为非泛型IEnumerable
而不是通用版IEnumerable<T>
更为“正常”)
public class EodSettlementRequestStubData : IEnumerable<SettlementRequest>
{
public SettlementRequest UnapprovedFlows;
public SettlementRequest UnapprovedRecovery;
public SettlementRequest UnverifiedFlows;
public SettlementRequest UnverifiedRecovery;
public IEnumerator<SettlementRequest> GetEnumerator()
{
yield return UnapprovedFlows;
yield return UnapprovedRecovery;
yield return UnverifiedFlows;
yield return UnverifiedRecovery;
}
IEnumerator IEnumerable.GetEnumerator()
{
//This is calling "IEnumerator<SettlementRequest> GetEnumerator()"
return GetEnumerator();
}
}