检查实例是否是给定类型的集合

时间:2023-01-12 17:01:15

Let's say I have this dummy code:

假设我有这个虚拟代码:

var object1 = new Test();

So, if I need check if object1 is an instance of Test class I can do:

所以,如果我需要检查object1是否是Test类的一个实例,我可以这样做:

var type = typeof(Test);
Console.WriteLine(object1.GetType() == type); // will print true

But now I have this object2 (A list of Test objects):

但是现在我有了这个对象2(一个Test对象列表):

var object2 = new List<Test>
{
    new Test(),
    new Test(),
    new Test()
};

My question is: How can I check if object2 is a list of Test instances?

我的问题是:如何检查object2是否是Test实例列表?

3 个解决方案

#1


1  

what about?

关于什么?

  Type myListElementType = object2.GetType().GetGenericArguments().Single();
  if (myListElementType  == typeof(Test))

#2


2  

You could either use .GetType() to afterwards compare it to the typeof(List<Test>) ..

您可以使用.GetType()将其与typeof(List )进行比较。

if (object2.GetType() == typeof(List<Test>))
{
    // do something
}

.. or you could use the is expression like:

..或者您可以使用is表达式:

if (object2 is List<Test>)
{
    // do something
}

These if-statements will be true if object2 is a List of Test-objects.

如果object2是Test-objects列表,则这些if语句将为true。

Note
Both fit for what you want to do but there are also some differences between .GetType(), typeof(..) and is. These are explained here: Type Checking: typeof, GetType, or is?

注意两者都适合您想要做的但是.GetType(),typeof(..)和is之间也存在一些差异。这里解释了这些:类型检查:typeof,GetType还是?

#3


0  

There is a more generic method of finding the type bound to the list by reflecting over the interfaces implemented on the underlying IList. This is an extension method I use for finding the bound type:

通过反映在底层IList上实现的接口,有一种更通用的方法来查找绑定到列表的类型。这是我用于查找绑定类型的扩展方法:

/// <summary>
/// Gets the underlying type bound to an IList. For example, if the list
/// is List{string}, the result will be typeof(string).
/// </summary>
public static Type GetBoundType( this IList list )
{
    Type type = list.GetType();

    Type boundType = type.GetInterfaces()
        .Where( x => x.IsGenericType )
        .Where( x => x.GetGenericTypeDefinition() == typeof(IList<>) )
        .Select( x => x.GetGenericArguments().First() )
        .FirstOrDefault();

    return boundType;
}

In the O.P.'s case:

在O.P.案中:

bool isBound = object2.GetBoundType() == typeof(Test);

#1


1  

what about?

关于什么?

  Type myListElementType = object2.GetType().GetGenericArguments().Single();
  if (myListElementType  == typeof(Test))

#2


2  

You could either use .GetType() to afterwards compare it to the typeof(List<Test>) ..

您可以使用.GetType()将其与typeof(List )进行比较。

if (object2.GetType() == typeof(List<Test>))
{
    // do something
}

.. or you could use the is expression like:

..或者您可以使用is表达式:

if (object2 is List<Test>)
{
    // do something
}

These if-statements will be true if object2 is a List of Test-objects.

如果object2是Test-objects列表,则这些if语句将为true。

Note
Both fit for what you want to do but there are also some differences between .GetType(), typeof(..) and is. These are explained here: Type Checking: typeof, GetType, or is?

注意两者都适合您想要做的但是.GetType(),typeof(..)和is之间也存在一些差异。这里解释了这些:类型检查:typeof,GetType还是?

#3


0  

There is a more generic method of finding the type bound to the list by reflecting over the interfaces implemented on the underlying IList. This is an extension method I use for finding the bound type:

通过反映在底层IList上实现的接口,有一种更通用的方法来查找绑定到列表的类型。这是我用于查找绑定类型的扩展方法:

/// <summary>
/// Gets the underlying type bound to an IList. For example, if the list
/// is List{string}, the result will be typeof(string).
/// </summary>
public static Type GetBoundType( this IList list )
{
    Type type = list.GetType();

    Type boundType = type.GetInterfaces()
        .Where( x => x.IsGenericType )
        .Where( x => x.GetGenericTypeDefinition() == typeof(IList<>) )
        .Select( x => x.GetGenericArguments().First() )
        .FirstOrDefault();

    return boundType;
}

In the O.P.'s case:

在O.P.案中:

bool isBound = object2.GetBoundType() == typeof(Test);