【问题标题】:If object is Generic List如果对象是通用列表
【发布时间】:2010-09-19 22:03:33
【问题描述】:

有没有办法确定一个对象是否是一个通用列表?我不会知道列表的类型,我只知道它是一个列表。我该如何确定?

【问题讨论】:

    标签: c# .net vb.net list generics


    【解决方案1】:

    System.Object 类中有一个 GetType() 函数。你试过吗?

    【讨论】:

    • 事件虽然这可能不是一个“完美”的答案,但我不明白为什么它被标记为负一。来吧伙计们,这不是最好的答案,但尽量不要损害某人的声誉。
    • GetType 将返回它是特定的通用列表,而不是它只是一个通用列表。
    【解决方案2】:

    试试:

    if(yourList.GetType().IsGenericType)
    {
      var genericTypeParams = yourList.GetType().GetGenericArguments;
      //do something interesting with the types..
    }
    

    【讨论】:

      【解决方案3】:

      这将返回“真”

      List<int> myList = new List<int>();
      
      Console.Write(myList.GetType().IsGenericType && myList is IEnumerable);
      

      您是否想知道它是否完全是一个“列表”...或者您是否可以接受它是 IEnumerable 和 Generic?

      【讨论】:

      • 目前无法编译。 dotnetfiddle.net/T0Q8w3。也许它在 08 年就做到了。
      • 嘿@sonicblis,“IEnumerable”在命名空间“System.Collections”中。您的小提琴中没有using System.Collections;。仍然在 2016 年编译 :)
      【解决方案4】:

      以下方法将返回泛型集合类型的项目类型。 如果该类型未实现 ICollection,则返回 null。

      static Type GetGenericCollectionItemType(Type type)
      {
          if (type.IsGenericType)
          {
              var args = type.GetGenericArguments();
              if (args.Length == 1 &&
                  typeof(ICollection<>).MakeGenericType(args).IsAssignableFrom(type))
              {
                  return args[0];
              }
          }
          return null;
      }
      

      编辑: 上述解决方案假定指定的类型有自己的泛型参数。这不适用于使用硬编码泛型参数实现 ICollection 的类型,例如:

      class PersonCollection : List<Person> {}
      

      这是一个处理这种情况的新实现。

      static Type GetGenericCollectionItemType(Type type)
      {
          return type.GetInterfaces()
              .Where(face => face.IsGenericType &&
                             face.GetGenericTypeDefinition() == typeof(ICollection<>))
              .Select(face => face.GetGenericArguments()[0])
              .FirstOrDefault();
      }
      

      【讨论】:

        【解决方案5】:

        这个问题模棱两可。

        答案取决于通用列表的含义。

        • 一个列表 ?

        • 从 List 派生的类?

        • 实现 IList 的类(在这种情况下,可以将数组视为通用列表 - 例如 int[] 实现 IList)?

        • 一个泛型并实现 IEnumerable 的类(这是accepted answer 中提出的测试)?但这也会将以下相当病态的类视为通用列表:

        .

        public class MyClass<T> : IEnumerable
        {
            IEnumerator IEnumerable.GetEnumerator()
            {
                return null;
            }
        }
        

        最佳解决方案(例如是否使用 GetType、IsAssignableFrom 等)取决于您的意思。

        【讨论】:

          【解决方案6】:

          接受的答案不保证 IList 的类型。 检查这个版本,它对我有用:

          private static bool IsList(object value)
          {
              var type = value.GetType();
              var targetType = typeof (IList<>);
              return type.GetInterfaces().Any(i => i.IsGenericType 
                                                && i.GetGenericTypeDefinition() == targetType);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多