示例#1
0
        static void Main(string[] args)
        {
            var del1 = new CovariantDelegate <MyParentType>(MyStaticMethodWithReturnParentType);
            var del2 = new CovariantDelegate <MyParentType>(MyStaticMethodWithReturnChildType);

            // Error
            //var del3 = new CovariantDelegate<MyChildType>(MyStaticMethodWithReturnParentType);
            var del4 = new CovariantDelegate <MyChildType>(MyStaticMethodWithReturnChildType);

            var del5 = new ContravariantDelegate <MyParentType>(MyStaticMethodWithInParentType);
            // Error
            //var del6 = new ContravariantDelegate<MyParentType>(MyStaticMethodWithInChildType);

            var del7 = new ContravariantDelegate <MyChildType>(MyStaticMethodWithInParentType);
            var del8 = new ContravariantDelegate <MyChildType>(MyStaticMethodWithInChildType);

            del1();
            del2.Invoke();
            del4();
            del5(new MyParentType());
            del7(new MyChildType());
            del8(new MyChildType());

            var myType = new MyType();

            var del9  = new CovariantDelegate <MyParentType>(myType.MyMethodWithReturnParentType);
            var del10 = new CovariantDelegate <MyParentType>(myType.MyMethodWithReturnChildType);

            del9();
            del10();

            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            #region 泛型调用
            //泛型委托
            InputGenericsDelegate <int> inputGenericsDelegate = (p) => { Console.WriteLine("泛型委托"); };

            //泛型类
            MyGenerics <string> myGenerics = new MyGenerics <string>();

            //泛型方法
            myGenerics.GenericsMethod <string>("泛型方法");
            myGenerics.GenericsMethod <string, int>("泛型方法");
            #endregion

            #region 泛型可变性
            //协变
            CovariantDelegate <string> covariantDelegateStr = () => { return("协变泛型委托"); };
            CovariantDelegate <object> covariantDelegate    = covariantDelegateStr;

            //逆变
            ContravariantDelegate <object> contravariantDelegateObj = (p) => { Console.WriteLine("逆变泛型委托"); };
            ContravariantDelegate <string> contravariantDelegate    = contravariantDelegateObj;
            #endregion

            Console.ReadKey();
        }
示例#3
0
        //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/using-variance-in-delegates
        //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/using-variance-for-func-and-action-generic-delegates

        static void Main(string[] args)
        {
            //Delegate Covariance / Contrvariance
            {
                Action <A> f1 = ProcessA;
                //Contrvariance - function which accepts less derived type, can be assigned to more derived type delegate
                //All Action delegate are contrvariant
                Action <B> f2 = f1;

                ContrvariantDelegate <A> f3 = ProcessA;
                //Direct contrvariant assignment is always supported (because it is basically wrapping in new Action())
                ContrvariantDelegate <B> f5 = ProcessA;
                //This won't compile if "in" keyword is missing in Delegate definition
                ContrvariantDelegate <B> f6 = f3;

                CovariantDelegate <B> f7 = ReturnB;
                //Direct covariant assignment is always supported (because it is basically wrapping in new Action())
                CovariantDelegate <A> f8 = ReturnB;
                //This won't compile if "out" keyword is missing in Delegate definition
                CovariantDelegate <A> f9 = f7;
            }

            //Collection Invariance
            //Those lines won't compile
            //List<A> list = new List<B>();
            //IList<A> list1 = new List<B>();
            //IEnumerable though supports covariance because you can not change it
            var             list2 = new List <B>();
            IEnumerable <A> list3 = list2;

            //Arrays support variance but can emmit runtime exception
            A[] array = new B[1];
            //Like in this case
            array[0] = new A();

            //Interface Covariance Contrvariance
            //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/creating-variant-generic-interfaces
            ICovariant <B> interfaceExample = null;
            //This won't compile without "out" keyword
            ICovariant <A> i = interfaceExample;

            IContrvariant <A> interfaceExample2 = null;
            //This won't compile without "in" keyword
            IContrvariant <B> i2 = interfaceExample2;

            //Accepts A, returns B
            IGenericInterface <A, B> interfaceExample3 = null;
            //Accepts B, returns A
            IGenericInterface <B, A> i3 = interfaceExample3;
            //Basically Contrvariance allows to accept more derived arguments and covariance to return less derived arguments
        }
示例#4
0
        static void Main1(string[] args)
        {
            Planet p = new Planet();
            Earth  e = new Earth();

            CovariantDelegate varCovariantDelegate = new CovariantDelegate(p.CallingPlanet);

            varCovariantDelegate();

            varCovariantDelegate = new CovariantDelegate(e.CallingEarth);
            varCovariantDelegate();

            ContravariantDelegate varContravariantDelegate = new ContravariantDelegate(PlanetParamMethod);

            varContravariantDelegate(e);

            varContravariantDelegate = new ContravariantDelegate(EarthParamMethod);
            varContravariantDelegate(e);
        }