private static void 泛型接口的抗变()
        {
            IDisplay <Shape>     shapeDisplay     = new ShapeDisplay();
            IDisplay <Rectangle> rectangleDisplay = shapeDisplay;

            IIndex <Rectangle> rectangles = RectangleCollection.GetRectangles();

            shapeDisplay.Show(rectangles[0]);
            rectangleDisplay.Show(rectangles[0]);
        }
        private static void 泛型接口的协变()
        {
            IIndex <Rectangle> rectangles = RectangleCollection.GetRectangles();
            //如果IIndex<out T> 去掉out  IIndex<Shape> shapes = rectangles; 编译错误,
            //Rectangle是Shape的子类 由子类泛型转换成基类泛型 需要协变(out T)?
            //IIndex<out T>  约定返回类型只能是T 在具体实现类中IIndex<Rectangle> 返回类型只能是Rectangle
            //Rectangle 是 Shape的子类 可以用父类的实例指向子类的引用进行编程
            IIndex <Shape> shapes = rectangles;

            for (int i = 0; i < shapes.Count; i++)
            {
                WriteLine(shapes[i]);
            }
        }
Exemple #3
0
        public void TestRectangleCollection()
        {
            // 协变
            IIndex <Rectangle> rectangles = RectangleCollection.GetRectangles();
            IIndex <Shape>     shapes     = rectangles;

            for (int i = 0; i < shapes.Count; i++)
            {
                Debug.WriteLine(shapes[i]);
            }

            // 抗变
            IDisplay <Shape>     shapeDisplay     = new ShapeDisplay();
            IDisplay <Rectangle> rectangleDisplay = shapeDisplay;

            Debug.WriteLine(rectangleDisplay.Show(rectangles[0]));
        }
 private DesignerGlyph GlyphFromPoint(Point point, out ActivityDesigner activityDesigner)
 {
     activityDesigner = null;
     WorkflowView parentView = base.ParentView;
     if (parentView != null)
     {
         RectangleCollection rectangles = new RectangleCollection();
         foreach (ActivityDesigner designer in this.GetActivityDesigners(parentView.ClientRectangleToLogical(new Rectangle(Point.Empty, parentView.ViewPortSize))))
         {
             if (!rectangles.IsPointInsideAnyRectangle(point))
             {
                 foreach (DesignerGlyph glyph in this.GetDesignerGlyphs(designer))
                 {
                     if (glyph.GetBounds(designer, false).Contains(point) && glyph.CanBeActivated)
                     {
                         activityDesigner = designer;
                         return glyph;
                     }
                 }
             }
             rectangles.AddRectangle(designer.Bounds);
         }
     }
     return null;
 }