Does Class need to implement IEnumerable to use Foreach

前端 未结 11 1720
慢半拍i
慢半拍i 2020-12-09 11:04

This is in C#, I have a class that I am using from some else\'s DLL. It does not implement IEnumerable but has 2 methods that pass back a IEnumerator. Is there a way I can u

11条回答
  •  囚心锁ツ
    2020-12-09 11:12

    Short answer:

    You need a class with a method named GetEnumerator, which returns the IEnumerator you already have. Achieve this with a simple wrapper:

    class ForeachWrapper
    {
      private IEnumerator _enumerator;
    
      public ForeachWrapper(Func enumerator)
      {
        _enumerator = enumerator;
      }
    
      public IEnumerator GetEnumerator()
      {
        return _enumerator();
      }
    }
    

    Usage:

    foreach (var element in new ForeachWrapper(x => myClass.MyEnumerator()))
    {
      ...
    }
    

    From the C# Language Specification:

    The compile-time processing of a foreach statement first determines the collection type, enumerator type and element type of the expression. This determination proceeds as follows:

    • If the type X of expression is an array type then there is an implicit reference conversion from X to the System.Collections.IEnumerable interface (since System.Array implements this interface). The collection type is the System.Collections.IEnumerable interface, the enumerator type is the System.Collections.IEnumerator interface and the element type is the element type of the array type X.

    • Otherwise, determine whether the type X has an appropriate GetEnumerator method:

      • Perform member lookup on the type X with identifier GetEnumerator and no type arguments. If the member lookup does not produce a match, or it produces an ambiguity, or produces a match that is not a method group, check for an enumerable interface as described below. It is recommended that a warning be issued if member lookup produces anything except a method group or no match.

      • Perform overload resolution using the resulting method group and an empty argument list. If overload resolution results in no applicable methods, results in an ambiguity, or results in a single best method but that method is either static or not public, check for an enumerable interface as described below. It is recommended that a warning be issued if overload resolution produces anything except an unambiguous public instance method or no applicable methods.

      • If the return type E of the GetEnumerator method is not a class, struct or interface type, an error is produced and no further steps are taken.

      • Member lookup is performed on E with the identifier Current and no type arguments. If the member lookup produces no match, the result is an error, or the result is anything except a public instance property that permits reading, an error is produced and no further steps are taken.

      • Member lookup is performed on E with the identifier MoveNext and no type arguments. If the member lookup produces no match, the result is an error, or the result is anything except a method group, an error is produced and no further steps are taken.

      • Overload resolution is performed on the method group with an empty argument list. If overload resolution results in no applicable methods, results in an ambiguity, or results in a single best method but that method is either static or not public, or its return type is not bool, an error is produced and no further steps are taken.

      • The collection type is X, the enumerator type is E, and the element type is the type of the Current property.

    • Otherwise, check for an enumerable interface:

      • If there is exactly one type T such that there is an implicit conversion from X to the interface System.Collections.Generic.IEnumerable, then the collection type is this interface, the enumerator type is the interface System.Collections.Generic.IEnumerator, and the element type is T.

      • Otherwise, if there is more than one such type T, then an error is produced and no further steps are taken.

      • Otherwise, if there is an implicit conversion from X to the System.Collections.IEnumerable interface, then the collection type is this interface, the enumerator type is the interface System.Collections.IEnumerator, and the element type is object.

      • Otherwise, an error is produced and no further steps are taken.

提交回复
热议问题