Determine if collection is of type IEnumerable

后端 未结 9 2011
轻奢々
轻奢々 2020-12-12 23:50

How to determine if object is of type IEnumerable ?

Code:

namespace NS {
    class Program {
        static IEnumerable GetInts()         


        
9条回答
  •  一生所求
    2020-12-13 00:12

    How to determine if object is of type IEnumerable ?

    Please feel free to use this fine, ultra small, generic extension method to determine if any object implements IEnumerable interface. It extends the Object type, so you can execute it using any instance of any object you're using.

    public static class CollectionTestClass
    {
        public static Boolean IsEnumerable(this Object testedObject)
        {
            return (testedObject is IEnumerable);
        }
    }
    

提交回复
热议问题