开发者

Create interface which derives from IEnumerable

开发者 https://www.devze.com 2022-12-14 06:51 出处:网络
I have a couple of classes all of them deriving from IQueryResult. I need each of those classes to be iterable in foreach loop. Unfortunately f开发者_JAVA技巧oreach loop cannot see GetEnumerator metho

I have a couple of classes all of them deriving from IQueryResult. I need each of those classes to be iterable in foreach loop. Unfortunately f开发者_JAVA技巧oreach loop cannot see GetEnumerator method. I've managed to use it in foreach using dynamic keyword available in .NET 4.0 but then IQueryResult need not to derive from IEnumerable.

public interface IQueryResult : IEnumerable<IQueryResult>
{
}

How would you do that in a more elegant way?

Kind Regards PK


Is an IQueryResult really IEnumerable<IQueryResult>? I might expect, for example, that (for a single grid):

public interface IQueryResult<T> : IEnumerable<T> { /* */ }

or if you are trying to represent multiple grids:

public interface IQueryResult<T> : IEnumerable<IEnumerable<T>> { /* */ }

Although I might be tempted to make it more explicit as a method:

public interface IQueryResult<T> {
    IEnumerable<IEnumerable<T>> GetGrids(); // or something
}

Note that you don't need IEnumerable[<T>] for foreach, but it makes life far more predictable, and makes it possible to use LINQ etc.


a. dont inherit ineterfaces.

b. you can use an excpilcit cast to IEnumarable on that class and it will work

0

精彩评论

暂无评论...
验证码 换一张
取 消