Skip to content

Instantly share code, notes, and snippets.

@takemyoxygen
Last active December 21, 2015 08:29
Show Gist options
  • Save takemyoxygen/6278583 to your computer and use it in GitHub Desktop.
Save takemyoxygen/6278583 to your computer and use it in GitHub Desktop.
class Program
{
class Foo<T> { }
class Bar<T> : Foo<T> { }
class Concrete : Bar<string> { }
class Another<T> : Foo<T> { }
static void Main(string[] args)
{
Type genericDefinition = typeof(Concrete).BaseType.GetGenericTypeDefinition(); // Bar<>
Console.WriteLine(genericDefinition.Name); // Bar`1
Console.WriteLine(typeof(Foo<>).IsAssignableFrom(genericDefinition)); // False. WHY?! :(
Console.WriteLine(genericDefinition.BaseType.Name); //Foo`1
Console.WriteLine(genericDefinition.BaseType == typeof(Foo<>)); // False. WHY?!
Console.WriteLine(genericDefinition.BaseType.GetGenericTypeDefinition() == typeof(Foo<>)); // Finally True!
Console.WriteLine(typeof(Another<>).GetGenericTypeDefinition().BaseType == typeof(Bar<>).GetGenericTypeDefinition().BaseType); // False
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment