Added a test scenario to verify that Nullable<T> is not assignable from T (dotnet...
authorVladimir Sadov <vsadov@microsoft.com>
Sun, 6 Oct 2019 22:39:00 +0000 (15:39 -0700)
committerGitHub <noreply@github.com>
Sun, 6 Oct 2019 22:39:00 +0000 (15:39 -0700)
* Added a test scenario to verify that T is not assignable to  Nullable<T>

* PR feedback. Replaced `var` with concrete `Type`.

Commit migrated from https://github.com/dotnet/corefx/commit/38aa9151607cb63bb569beb42880057ba2cd3da0

src/libraries/System.Reflection/tests/TypeInfoTests.cs

index 990cf9d..76989f6 100644 (file)
@@ -574,6 +574,33 @@ namespace System.Reflection.Tests
             Assert.Equal(expected, type.GetTypeInfo().IsAssignableFrom(c?.GetTypeInfo()));
         }
 
+        [Fact]
+        public void IsAssignableFromNullable()
+        {
+            Type nubInt = typeof(Nullable<int>);
+            Type intType = typeof(int);
+            Type objType = typeof(object);
+            Type valTypeType = typeof(ValueType);
+
+            // sanity checks
+            // Nullable<T>  is assignable from  int
+            Assert.True(nubInt.IsAssignableFrom(intType));
+            Assert.False(intType.IsAssignableFrom(nubInt));
+
+            Type nubOfT = nubInt.GetGenericTypeDefinition();
+            Type T = nubOfT.GetTypeInfo().GenericTypeParameters[0];
+
+            // should be true
+            Assert.True(T.IsAssignableFrom(T));
+            Assert.True(objType.IsAssignableFrom(T));
+            Assert.True(valTypeType.IsAssignableFrom(T));
+
+            // should be false
+            // Nullable<T> is not assignable from T
+            Assert.False(nubOfT.IsAssignableFrom(T));
+            Assert.False(T.IsAssignableFrom(nubOfT));
+        }
+
         public static IEnumerable<object[]> IsEquivilentTo_TestData()
         {
             yield return new object[] { typeof(string), typeof(string), true };