Пример #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Application.Idle += new EventHandler(Application_Idle);

            IEngine engine = new Engine("AopDraw");

            InterfaceAspect canvasAwareAspect = new InterfaceAspect("canvasAwareAspect", typeof(Shape), new Type[] { typeof(CanvasAwareMixin) }, new IPointcut[] { new SignaturePointcut("set_*", new ShapePropertyInterceptor()) });

            engine.Configuration.Aspects.Add(canvasAwareAspect);

            InterfaceAspect typeDescriptorAspect = new InterfaceAspect("typeDescriptorAspect", typeof(Shape), new Type[] { typeof(CustomTypeDescriptorMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(typeDescriptorAspect);

            InterfaceAspect guidAspect = new InterfaceAspect("guidAspect", typeof(Shape), new Type[] { typeof(GuidObject) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(guidAspect);

            AttributeAspect resizableAspect = new AttributeAspect("resizableAspect", typeof(ResizableAttribute), new Type[] { typeof(ResizableShape2DMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(resizableAspect);

            AttributeAspect selectable2DAspect = new AttributeAspect("selectable2DAspect", typeof(SelectableAttribute), new Type[] { typeof(SelectableShape2DMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(selectable2DAspect);

            InterfaceAspect selectable1DAspect = new InterfaceAspect("selectable1DAspect", typeof(Shape1D), new Type[] { typeof(SelectableShape1DMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(selectable1DAspect);

            InterfaceAspect mouseEventAspect = new InterfaceAspect("mouseEventAspect", typeof(Shape), new Type[] { typeof(MouseHandlerMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(mouseEventAspect);

            AttributeAspect movableAspect = new AttributeAspect("movableAspect", typeof(MovableAttribute), new Type[] { typeof(MovableShape2DMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(movableAspect);

            InterfaceAspect movableShape1DAspect = new InterfaceAspect("movableShape1DAspect", typeof(Shape1D), new Type[] { typeof(MovableShape1DMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(movableShape1DAspect);

            AttributeAspect designableAspect = new AttributeAspect("designableAspect", typeof(DesignableAttribute), new Type[] { typeof(DesignableMixin) }, new IPointcut[] { });

            engine.Configuration.Aspects.Add(designableAspect);



            SquareShape square = engine.CreateProxy <SquareShape>();

            square.X      = 10;
            square.Y      = 10;
            square.Width  = 100;
            square.Height = 100;

            canvas.AddShape(square);


            CircleShape circle = engine.CreateProxy <CircleShape>();

            circle.X      = 240;
            circle.Y      = 120;
            circle.Width  = 200;
            circle.Height = 200;

            canvas.AddShape(circle);


            rectangle        = engine.CreateProxy <RectangleShape>();
            rectangle.X      = 50;
            rectangle.Y      = 90;
            rectangle.Width  = 200;
            rectangle.Height = 50;

            canvas.AddShape(rectangle);

            Line line = engine.CreateProxy <Line>();

            line.X  = 200;
            line.Y  = 200;
            line.X2 = 400;
            line.Y2 = 340;
            canvas.AddShape(line);


            TextShape text = engine.CreateProxy <TextShape>();

            text.X      = 140;
            text.Y      = 3;
            text.Width  = 200;
            text.Height = 100;
            canvas.AddShape(text);
        }
        /// <summary>
        /// return a configured <c>IEngine</c> from an xml element.
        /// </summary>
        /// <param name="xmlRoot">xml node to deserialize</param>
        /// <returns>a configured <c>IEngine</c></returns>
        public IEngine Configure(XmlElement xmlRoot)
        {
            Engine engine = new Engine("App.Config");

            XmlElement o = xmlRoot;

            if (o == null)
            {
                return(engine);
            }


            foreach (XmlNode settingsNode in o)
            {
                if (settingsNode.Name == "aspect")
                {
                    IList pointcuts = new ArrayList();
                    IList mixins    = new ArrayList();

                    string aspectName = settingsNode.Attributes["name"].Value;


                    foreach (XmlNode aspectNode in settingsNode)
                    {
                        if (aspectNode.Name == "pointcut")
                        {
                            IList interceptors = new ArrayList();

                            foreach (XmlNode pointcutNode in aspectNode)
                            {
                                if (pointcutNode.Name == "interceptor")
                                {
                                    string typeString      = pointcutNode.Attributes["type"].Value;
                                    Type   interceptorType = Type.GetType(typeString);
                                    if (interceptorType == null)
                                    {
                                        throw new Exception(
                                                  string.Format("Interceptor type '{0}' was not found!", typeString));
                                    }
                                    object interceptor = Activator.CreateInstance(interceptorType);
                                    interceptors.Add(interceptor);
                                }
                            }

                            IPointcut pointcut = null;
                            if (aspectNode.Attributes["target-signature"] != null)
                            {
                                string targetMethodSignature = aspectNode.Attributes["target-signature"].Value;
                                pointcut = new SignaturePointcut(targetMethodSignature, interceptors);
                            }

                            if (aspectNode.Attributes["target-attribute"] != null)
                            {
                                string attributeTypeString = aspectNode.Attributes["target-attribute"].Value;
                                Type   attributeType       = Type.GetType(attributeTypeString);
                                if (attributeType == null)
                                {
                                    throw new Exception(
                                              string.Format("Attribute type '{0}' was not found!", attributeTypeString));
                                }

                                pointcut = new AttributePointcut(attributeType, interceptors);
                            }


                            pointcuts.Add(pointcut);
                        }

                        if (aspectNode.Name == "mixin")
                        {
                            string typeString = aspectNode.Attributes["type"].Value;
                            Type   mixinType  = Type.GetType(typeString);
                            if (mixinType == null)
                            {
                                throw new Exception(string.Format("Mixin type '{0}' was not found!", typeString));
                            }
                            mixins.Add(mixinType);
                        }
                    }

                    IGenericAspect aspect = null;

                    if (settingsNode.Attributes["target-signature"] != null)
                    {
                        string targetTypeSignature = settingsNode.Attributes["target-signature"].Value;
                        aspect = new SignatureAspect(aspectName, targetTypeSignature, mixins, pointcuts);
                    }

                    if (settingsNode.Attributes["target-attribute"] != null)
                    {
                        string attributeTypeString = settingsNode.Attributes["target-attribute"].Value;
                        Type   attributeType       = Type.GetType(attributeTypeString);

                        aspect = new AttributeAspect(aspectName, attributeType, mixins, pointcuts);
                    }

                    engine.Configuration.Aspects.Add(aspect);
                }
            }


            return(engine);
        }