public void ProvideAdvices(Weaver codeWeaver)
        {
            // Gets the dictionary of custom attributes.
            AnnotationRepositoryTask customAttributeDictionary =
                AnnotationRepositoryTask.GetTask(this.Project);

            // Requests an enumerator of all instances of our Singleton.
            IEnumerator<IAnnotationInstance> customAttributeEnumerator =
                customAttributeDictionary.GetAnnotationsOfType(typeof (SingletonAttribute), true);

            ICollection<TypeDefDeclaration> singletons = new HashSet<TypeDefDeclaration>();
            // For each instance of our Singleton.
            while (customAttributeEnumerator.MoveNext())
            {
                // Gets the type to which it applies.
                TypeDefDeclaration typeDef = customAttributeEnumerator.Current.TargetElement
                                             as TypeDefDeclaration;

                if (typeDef != null && !singletons.Contains(typeDef))
                {
                    singletons.Add(typeDef);

                    codeWeaver.AddTypeLevelAdvice(new SingletonAccessorAdvice(typeDef),
                                                  JoinPointKinds.BeforeStaticConstructor,
                                                  new Singleton<TypeDefDeclaration>(typeDef));
                    codeWeaver.AddMethodLevelAdvice(new SingletonAdvice(typeDef),
                                                    null,
                                                    JoinPointKinds.InsteadOfNewObject,
                                                    new Singleton<MetadataDeclaration>(
                                                        typeDef.Methods.GetOneByName(".ctor")));
                }
            }
            singletons.Clear();

            foreach (AssemblyRefDeclaration assembly in this.Project.Module.AssemblyRefs)
            {
                foreach (TypeRefDeclaration type in assembly.TypeRefs)
                {
                    TypeDefDeclaration def = type.GetTypeDefinition();
                    foreach (CustomAttributeDeclaration att in def.CustomAttributes)
                    {
                        if (Equals(att.Constructor.DeclaringType.GetSystemType(new Type[] {}, new Type[] {}),
                                   typeof (SingletonAttribute)))
                        {
                            singletons.Add(def);
                        }
                    }
                }
            }

            foreach (TypeDefDeclaration type in singletons)
            {
                codeWeaver.AddMethodLevelAdvice(new SingletonAdvice(type),
                                                null,
                                                JoinPointKinds.InsteadOfNewObject,
                                                new Singleton<MetadataDeclaration>(type.Methods.GetOneByName(".ctor")));
            }
        }
Ejemplo n.º 2
0
        public void ProvideAdvices(Weaver codeWeaver)
        {
            CustomAttributeDictionaryTask customAttributeDictionary = CustomAttributeDictionaryTask.GetTask(this.Project);

            IEnumerator<ICustomAttributeInstance> customAttributeEnumerator =
                customAttributeDictionary.GetCustomAttributesEnumerator(typeof(NotifyPropertyChangedAttribute), false);

            while (customAttributeEnumerator.MoveNext())
            {
                PropertyDeclaration propertyDeclaration = customAttributeEnumerator.Current.TargetElement as PropertyDeclaration;

                if (propertyDeclaration == null || propertyDeclaration.Visibility == Visibility.Private)
                    continue;

                MethodDefDeclaration getMethodDef;
                MethodDefDeclaration setMethodDef;

                GetPropertyAccessorDefinitions(propertyDeclaration, out getMethodDef, out setMethodDef);
                if (getMethodDef == null || setMethodDef == null)
                {
                    continue;
                }

                NotifyPropertyChangedAdvice advice = new NotifyPropertyChangedAdvice(
                    getMethodDef,
                    setMethodDef);

                codeWeaver.AddMethodLevelAdvice(advice,
                                                 new Singleton<MethodDefDeclaration>(setMethodDef),
                                                 JoinPointKinds.BeforeMethodBody |
                                                 JoinPointKinds.AfterMethodBodySuccess,
                                                 null);
            }
        }
        public void ProvideAdvices(Weaver codeWeaver)
        {
            // Gets the dictionary of custom attributes.
            AnnotationRepositoryTask customAttributeDictionary =
                AnnotationRepositoryTask.GetTask(this.Project);

            // Requests an enumerator of all instances of our NonNullAttribute.
            IEnumerator<IAnnotationInstance> customAttributeEnumerator =
                customAttributeDictionary.GetAnnotationsOfType(typeof (NonNullAttribute), true);
            // Simulating a Set
            IDictionary<MethodDefDeclaration, MethodDefDeclaration> methods =
                new Dictionary<MethodDefDeclaration, MethodDefDeclaration>();
            // For each instance of our NonNullAttribute.
            while (customAttributeEnumerator.MoveNext())
            {
                // Gets the parameters to which it applies.
                ParameterDeclaration paramDef = customAttributeEnumerator.Current.TargetElement
                                                as ParameterDeclaration;

                if (paramDef != null)
                {
                    if ((paramDef.Attributes & ParameterAttributes.Retval) == ParameterAttributes.Retval)
                    {
                        codeWeaver.AddMethodLevelAdvice(new NonNullReturnAdvice(),
                                                        new Singleton<MethodDefDeclaration>(paramDef.Parent),
                                                        JoinPointKinds.AfterMethodBodySuccess,
                                                        null);
                    }
                    else
                    {
                        if (!methods.ContainsKey(paramDef.Parent))
                        {
                            codeWeaver.AddMethodLevelAdvice(new NonNullParameterAdvice(paramDef),
                                                            new Singleton<MethodDefDeclaration>(paramDef.Parent),
                                                            JoinPointKinds.BeforeMethodBody,
                                                            null);
                            methods.Add(paramDef.Parent, paramDef.Parent);
                        }
                    }
                }
            }
        }
        public void ProvideAdvices(Weaver codeWeaver)
        {
            // Gets the dictionary of custom attributes.
            CustomAttributeDictionaryTask customAttributeDictionary =
                CustomAttributeDictionaryTask.GetTask(this.Project);

            // Requests an enumerator of all instances of our NonEmptyAttribute.
            IEnumerator<ICustomAttributeInstance> customAttributeEnumerator =
                customAttributeDictionary.GetCustomAttributesEnumerator(typeof(NonEmptyAttribute), true);

            // For each instance of our NonEmptyAttribute.
            while (customAttributeEnumerator.MoveNext())
            {
                // Gets the parameters to which it applies.
                ParameterDeclaration paramDef = customAttributeEnumerator.Current.TargetElement
                                                 as ParameterDeclaration;

                if (paramDef != null)
                {
                    if ((paramDef.Attributes & ParameterAttributes.Retval) == ParameterAttributes.Retval)
                    {
                        codeWeaver.AddMethodLevelAdvice(new NonEmptyReturnAdvice(),
                                                         new Singleton<MethodDefDeclaration>(paramDef.Parent),
                                                         JoinPointKinds.AfterMethodBodySuccess,
                                                         null);
                    }
                    else
                    {
                        codeWeaver.AddMethodLevelAdvice(new NonEmptyParameterAdvice(),
                                                         new Singleton<MethodDefDeclaration>(paramDef.Parent),
                                                         JoinPointKinds.BeforeMethodBody,
                                                         null);
                    }
                }
            }
        }