Exemple #1
0
        /// <summary>
        /// 为了解决泛型父子类 关系的实例进行转换
        /// 协变,逆变;只能放在接口或者委托的泛型参数前面
        /// 协变:out
        ///       只以做为返回值
        ///       左边是父,右边是子
        /// 逆变:in
        ///       只可以做为参数值
        ///       左边是子,右边是父
        /// </summary>
        public void Test()
        {
            {
                parent parent1 = new parent();
                parent parent2 = new child(); //父类拥有子类的所有属性
                child  child1  = new child();
            }

            {
                List <Worker> parentlist1 = new List <Worker>();
                //List<Worker> parentlist2 = new List<Worker>();   //两个list 泛型实例不存在集成关系

                // 为解决两个父子关系的实例采用协变进行;
                List <Worker> parentlist2 = new List <WorkBase>().Select(item => (Worker)item).ToList();

                ///协变 out 处理左侧是父,右侧是子
                IEnumerable <WorkBase> outList   = new List <Worker>();
                IMyOut <WorkBase>      outMyList = new MyOut <Worker>();

                //逆变 in 处理左侧是子,右侧是父
                IMyIn <Worker> inList = new MyIn <WorkBase>();


                //协变+逆变
                IMyInOut <Worker, WorkBase> myInOut = new MyInOut <WorkBase, Worker>();
                //协变
                IMyInOut <Worker, WorkBase> myInOut2 = new MyInOut <Worker, Worker>();
                //逆变
                IMyInOut <Worker, WorkBase> myInOut3 = new MyInOut <WorkBase, WorkBase>();
            }
        }
Exemple #2
0
        public static T Log <T>(T data, string id, string file, int line, int reason)
        {
            // Gather all the pending Console.Writes from the app, via our hooked side-effect Console.Out monoid
            var s = MyOut.Log()?.Replace("\\", "\\\\").Replace("\r", "\\r").Replace("\n", "\\n");

            if (s != null)
            {
                Queue.Post(new LineItem(file, line, $"\"{s}\""));
            }

            // Log this current event (for declarations and expression statements)
            s  = (id == null) ? "" : id + "=";
            s += (data == null) ? "null" : data.ToString();
            if (reason == 1 || reason == 2)
            {
                Queue.Post(new LineItem(file, line, s));
            }

            return(data);
        }