コード例 #1
0
        public void GenericTypeIsACompositionOfTypes()
        {
            var helloCat = new SayHello <Cat> ();

            //Assert.AreEqual (typeof(FILL_ME_IN), helloCat.GetType ());
            Assert.AreEqual(typeof(SayHello <Cat>), helloCat.GetType());
        }
コード例 #2
0
        public void IsGenericTypeInformation()
        {
            var helloCat = new SayHello <Cat> ();

            Assert.AreEqual(true, helloCat.GetType().IsGenericType);
            Assert.AreEqual(true, typeof(SayHello <>).IsGenericTypeDefinition);
        }
コード例 #3
0
        public void GetGenericTypeFromUsage()
        {
            var helloCat     = new SayHello <Cat> ();
            var expectedType = typeof(SayHello <>);

            Assert.AreEqual(expectedType, helloCat.GetType().GetGenericTypeDefinition());
            Assert.AreEqual(true, typeof(SayHello <>).IsGenericTypeDefinition);
        }
コード例 #4
0
        public void ComposedTypesShareTheSameGenericTypeDefinition()
        {
            var helloCat = new SayHello <Cat> ();
            var helloDog = new SayHello <Dog> ();

            var genericTypeFromDogToCompare = helloDog.GetType().GetGenericTypeDefinition();

            Assert.AreEqual(genericTypeFromDogToCompare, helloCat.GetType().GetGenericTypeDefinition());
        }
コード例 #5
0
        public void GenericTypeParameterCanRestrictedToCertainTypes()
        {
            // Podeu escriure això:
            var helloInt = new SayHello <int> ();

            // Però no podeu escriure això, perquè int no hereta d'Animal:
            // var helloAnimalInt = new SayHelloToAnimalsOnly<int> ();

            // També us podeu assegurar que:
            //- és una classe:
            //     public class MyGeneric<T> where T : class
            //- té un constructor:
            //     public class MyGeneric<T> where T : new()
            //- podeu afegir condicions múltiples:
            //     public class MyGeneric<T> where T : Animal, class

            //Assert.AreEqual (true, FILL_ME_IN);
            Assert.AreEqual(true, helloInt.GetType().GetGenericTypeDefinition().IsGenericTypeDefinition);
        }