The code below is a simplification to show what's happening. When declaring b1 specifically as the base type, t1 is Base type and t2 is Derived type. When b2 is declared with var, both t1 and t2 are Derived type. So this means that the generic code generated for handling b1 works with the base type. Is this by design, and why? And is there a way around it, so with b1, t1 and t2 are both Derived type (without changing to var)?
class Base { }
class Derived : Base { }
class Program
{
static void Main()
{
Base b1 = new Derived();
var b2 = new Derived();
SomeMethod(b1);
SomeMethod(b2);
}
private static void SomeMethod<T>(T obj) where T : Base
{
var t1 = typeof(T);
var t2 = obj.GetType();
}
}