Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare object's type with a generics type, irrelevant to generic argument?

Tags:

c#

generics

Best way to illustrate my question is with this example code:

  class Item {}
  class Container< T > {}
  class Program
  {
    static void DoSomething( object something )
    {
      if( typeof( Item ) == something.GetType() )
      {
        System.Console.WriteLine( "Item" );
      }
      else if( typeof( Container<> ) == something.GetType() )
      {
        System.Console.WriteLine( "Container<>" );
      }
    }

    static void Main( string[] args )
    {
      DoSomething( new Item() );
      DoSomething( new Container< int >() );
    }
  }

The following line will not work:

else if( typeof( Container<> ) == something.GetType() )

Is it a way to make it work without explicitly changing Container<> into Container<int>? I want to know that object is of 'Container' type and I really has no interest is it Container<int> or Container<string>. Any hints other than dozens lines of reflection?

like image 537
grigoryvp Avatar asked Dec 06 '09 12:12

grigoryvp


People also ask

How does a generic method differ from a generic type?

From the point of view of reflection, the difference between a generic type and an ordinary type is that a generic type has associated with it a set of type parameters (if it is a generic type definition) or type arguments (if it is a constructed type). A generic method differs from an ordinary method in the same way.

How do you know if an object is generic?

If you want to check if it's an instance of a generic type: return list. GetType().

What is a generic argument?

Generic arguments, or arguments applied to an entire class or group of opposing arguments, occur frequently in academic debate. Many generic argument positions endure across debate resolutions.

Can dynamic type be used for generic?

Not really. You need to use reflection, basically. Generics are really aimed at static typing rather than types only known at execution time.


1 Answers

Try:

typeof(Container<>) == something.GetType().GetGenericTypeDefinition()

Note that this will only return true if the actual type is Container<T>. It doesn't work for derived types. For instance, it'll return false for the following:

class StringContainer : Container<string>

If you need to make it work for this case, you should traverse the inheritance hierarchy and test each base class for being Container<T>:

static bool IsGenericTypeOf(Type genericType, Type someType)
{   
  if (someType.IsGenericType 
          && genericType == someType.GetGenericTypeDefinition()) return true;

  return someType.BaseType != null 
          && IsGenericTypeOf(genericType, someType.BaseType);
}
like image 78
mmx Avatar answered Oct 19 '22 21:10

mmx