Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does checking if a Type of System.Int32 is assignable to Type of INumber<> return false in a unit test?

This statement when run from a console application sets 'x' to true:

var x = 3.GetType().IsAssignableTo(typeof(INumber<>)); // x == true

The same statement when run inside a unit test sets x to false. Why?

var x = 3.GetType().IsAssignableTo(typeof(INumber<>));

like image 712
rory.ap Avatar asked Sep 06 '25 08:09

rory.ap


1 Answers

This was a bug that was introduced in .NET 6, and fixed in .NET 8.

The correct behaviour is that uninstantiated generics are not AssignableTo.

I guess this makes sense, as you can't write INumber<> x = 3.

like image 121
canton7 Avatar answered Sep 07 '25 22:09

canton7