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?