What is the best way to convert an IEnumerator to a generic IEnumerator?

前端 未结 5 766
误落风尘
误落风尘 2020-12-06 09:28

I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and I am wanting to expose the IEnumerator as a generic IEnumerator.

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 09:59

    IEnumerable already derives from IEnumerable so there's no need to do any conversion. You can simply cast to it...well actually it's implicit no cast necessary.

    IEnumerable enumerable = GetGenericFromSomewhere();
    IEnumerable sadOldEnumerable = enumerable;
    return sadOldEnumerable.GetEnumerator();
    

    Going the other way round isn't much more difficult with LINQ:

    var fancyEnumerable = list.OfType();
    return fancyEnumerable.GetEnumerator();
    

提交回复
热议问题