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());
        }
        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);
        }