Can someone explain this? Per documentation IsGenericType
indicatesing whether the current Type represents a type parameter in the definition of a generic type or method.
So this (LINQPad) code:
bool bStraight = typeof(Task).IsGenericType;
bStraight.Dump("typeof(Task).IsGenericType");
works as expected and yields the output:
typeof(Task).IsGenericType
False
But when I retrieve it from a method by reflection:
public class MyClass
{
public async Task Method()
{
await Task.Run(() =>
{
Thread.Sleep(3000);
});
}
}
public async Task TEST()
{
MyClass theObject = new MyClass();
Task task = (Task)typeof(MyClass).GetTypeInfo()
.GetDeclaredMethod("Method")
.Invoke(theObject, null);
bool b = task.GetType().IsGenericType;
bool b2 = task.GetType().GetGenericTypeDefinition() == typeof(Task<>);
b.Dump("IsGenericType");
b2.Dump("GetGenericTypeDefinition");
bool bStraight = typeof(Task).IsGenericType;
bStraight.Dump("typeof(Task).IsGenericType");
}
I get the unexpected output of :
IsGenericType
TrueGetGenericTypeDefinition
True