Checking if Type or instance implements IEnumerable regardless of Type T

后端 未结 3 795
無奈伤痛
無奈伤痛 2020-12-16 09:38

I\'m doing a heavy bit of reflection in my current project, and I\'m trying to provide a few helper methods just to keep everything tidy.

I\'d like to provide a pair

3条回答
  •  清歌不尽
    2020-12-16 10:10

    The following line

    return (type is IEnumerable);
    

    is asking "if an instance of Type, type is IEnumerable", which clearly it is not.

    You want to do is:

    return typeof(IEnumerable).IsAssignableFrom(type);
    

提交回复
热议问题