Beispiel #1
0
    public void Execute()
    {
        this.LogInfo    = s => { };
        this.LogWarning = s => { };

        var decorator = new MethodDecorator.Fody.MethodDecorator(this.ModuleDefinition, Config);

        foreach (var x in this.ModuleDefinition.AssemblyReferences)
        {
            AssemblyResolver.Resolve(x);
        }

        this.DecorateDirectlyAttributed(decorator);
        this.DecorateAttributedByImplication(decorator);

        if (this.ModuleDefinition.AssemblyReferences.Count(r => r.Name == "mscorlib") > 1)
        {
            throw new Exception(
                      String.Format(
                          "Error occured during IL weaving. The new assembly is now referencing more than one version of mscorlib: {0}",
                          String.Join(", ", this.ModuleDefinition.AssemblyReferences.Where(r => r.Name == "mscorlib").Select(r => r.FullName))
                          )
                      );
        }
    }
    private void DecorateByType(MethodDecorator.Fody.MethodDecorator decorator)
    {
        var referenceFinder       = new ReferenceFinder(this.ModuleDefinition);
        var markerTypeDefinitions = this.FindMarkerTypes();

        // Look for rules in the assembly and module.
        var assemblyRules = FindAspectRules(this.ModuleDefinition.Assembly.CustomAttributes);
        var moduleRules   = FindAspectRules(this.ModuleDefinition.CustomAttributes);

        // Read the top-level and nested types from this module
        foreach (var type in this.ModuleDefinition.Types.SelectMany(x => GetAllTypes(x)))
        {
            // Look for rules on the type and marker attributes
            var classRules = FindByMarkerType(markerTypeDefinitions, type.CustomAttributes)
                             .Concat(FindAspectRules(type.CustomAttributes, true));

            // Loop through all methods in this type
            foreach (var method in type.Methods.Where(x => x.HasBody))
            {
                // Find any rules applied to the method.
                var methodRules = FindByMarkerType(markerTypeDefinitions, method.CustomAttributes)
                                  .Concat(FindAspectRules(method.CustomAttributes, true));

                // Join together all the rules and give them an ordering number starting at 0 for
                // the highest level (assembly) to N as a lowest level (last attribute on the method)
                var allRules = assemblyRules
                               .Concat(moduleRules)
                               .Concat(classRules)
                               .Concat(methodRules)
                               .Select((Rule, ScopeOrdering) => new { Rule, ScopeOrdering });

                // Group the rules by the aspect type
                foreach (var aspectSet in
                         allRules.ToLookup(x => x.Rule.MethodDecoratorAttribute.AttributeType))
                {
                    // Sort the rules in priority order (so that attributes applied to the
                    // method take precedence over the type, module then assembly)
                    // Then pick out the first rule - this tells us whether to include
                    // or exclude.
                    var ruleList = aspectSet
                                   .Where(x => x.Rule.Match(type, method))
                                   .OrderBy(x => x.Rule.AttributePriority)              // Apply lowest priority number first
                                   .ThenByDescending(x => x.ScopeOrdering)              // Method rules sort 1st
                                   .Select(x => x.Rule);

                    var rule = ruleList.FirstOrDefault();

                    // If we have a rule and it isn't an exclusion, apply the method decoration.
                    if (rule != null && !rule.AttributeExclude)
                    {
                        decorator.Decorate(
                            type,
                            method,
                            rule.MethodDecoratorAttribute);
                    }
                }
            }
        }
    }
Beispiel #3
0
    private void DecorateDirectlyAttributed(MethodDecorator.Fody.MethodDecorator decorator)
    {
        var markerTypeDefinitions = this.FindMarkerTypes();

        var methods = this.FindAttributedMethods(markerTypeDefinitions.ToArray());

        foreach (var x in methods)
        {
            decorator.Decorate(x.TypeDefinition, x.MethodDefinition, x.CustomAttribute);
        }
    }
    public void Execute()
    {
        LogInfo = s => { };
        LogWarning = s => { };

        var markerTypeDefinitions = FindMarkerTypes();

        var decorator = new MethodDecorator.Fody.MethodDecorator(ModuleDefinition);

        var methods = FindAttributedMethods(markerTypeDefinitions);
        foreach (var method in methods)
            decorator.Decorate(method.Item1, method.Item2);
    }
    public override void Execute()
    {
        this.LogInfo    = s => { };
        this.LogWarning = s => { };

        var decorator = new MethodDecorator.Fody.MethodDecorator(this.ModuleDefinition);

        foreach (var x in this.ModuleDefinition.AssemblyReferences)
        {
            ResolveAssembly(x.FullName);
        }

        this.DecorateAttributedByImplication(decorator);
        this.DecorateByType(decorator);
    }
Beispiel #6
0
    public void Execute()
    {
        LogInfo    = s => { };
        LogWarning = s => { };

        var markerTypeDefinitions = FindMarkerTypes();

        var decorator = new MethodDecorator.Fody.MethodDecorator(ModuleDefinition);

        var methods = FindAttributedMethods(markerTypeDefinitions);

        foreach (var method in methods)
        {
            decorator.Decorate(method.Item1, method.Item2);
        }
    }
Beispiel #7
0
    private void DecorateAttributedByImplication(MethodDecorator.Fody.MethodDecorator decorator)
    {
        var inderectAttributes = this.ModuleDefinition.CustomAttributes
                                 .Concat(this.ModuleDefinition.Assembly.CustomAttributes)
                                 .Where(x => x.AttributeType.Name.StartsWith("IntersectMethodsMarkedByAttribute"))
                                 .Select(ToHostAttributeMapping)
                                 .Where(x => x != null)
                                 .ToArray();

        foreach (var inderectAttribute in inderectAttributes)
        {
            var methods = this.FindAttributedMethods(inderectAttribute.AttribyteTypes);
            foreach (var x in methods)
            {
                decorator.Decorate(x.TypeDefinition, x.MethodDefinition, inderectAttribute.HostAttribute);
            }
        }
    }
Beispiel #8
0
    private void DecorateByType(MethodDecorator.Fody.MethodDecorator decorator)
    {
        var byTypeAttributes = this.ModuleDefinition.CustomAttributes
                               .Concat(this.ModuleDefinition.Assembly.CustomAttributes)
                               .Where(x => x.AttributeType.Resolve().Implements(typeof(IMethodDecoratorByType)))
                               .Distinct()
                               .Cast <CustomAttribute>()
                               .ToArray();

        foreach (var attribute in byTypeAttributes)
        {
            var types         = new List <TypeDefinition>();
            var typesArgument = attribute.Properties.FirstOrDefault(arg => arg.Name == "Types").Argument;
            if (typesArgument.Type != null)
            {
                types = ((CustomAttributeArgument[])typesArgument.Value).Select(v => v.Value).Cast <TypeDefinition>().ToList();
            }

            var applyToInheritedTypes    = true;
            var applyToInheritedArgument = attribute.Properties.FirstOrDefault(arg => arg.Name == "ApplyToInheritedTypes").Argument;
            if (applyToInheritedArgument.Type != null)
            {
                applyToInheritedTypes = (bool)applyToInheritedArgument.Value;
            }

            var onlyPublicMethods  = true;
            var onlyPublicArgument = attribute.Properties.FirstOrDefault(arg => arg.Name == "OnlyDecoratePublicMethods").Argument;
            if (onlyPublicArgument.Type != null)
            {
                onlyPublicMethods = (bool)onlyPublicArgument.Value;
            }

            if (types.Count > 0)
            {
                var methods = this.FindMethodsByType(types, onlyPublicMethods, applyToInheritedTypes);
                foreach (var x in methods)
                {
                    decorator.Decorate(x.TypeDefinition, x.MethodDefinition, attribute);
                }
            }
        }
    }
Beispiel #9
0
    public void Execute()
    {
        this.LogInfo = s => { };
        this.LogWarning = s => { };

        var decorator = new MethodDecorator.Fody.MethodDecorator(this.ModuleDefinition);

        foreach (var x in this.ModuleDefinition.AssemblyReferences) AssemblyResolver.Resolve(x);

        this.DecorateDirectlyAttributed(decorator);
        this.DecorateAttributedByImplication(decorator);

        if(this.ModuleDefinition.AssemblyReferences.Count(r => r.Name == "mscorlib") > 1) {
            throw new Exception(
                String.Format(
                    "Error occured during IL weaving. The new assembly is now referencing more than one version of mscorlib: {0}",
                    String.Join(", ", this.ModuleDefinition.AssemblyReferences.Where(r => r.Name == "mscorlib").Select(r => r.FullName))
                )
            );
        }
    }
Beispiel #10
0
    private void DecorateDirectlyAttributed(MethodDecorator.Fody.MethodDecorator decorator)
    {
        var markerTypeDefinitions = this.FindMarkerTypes();

        var methods = this.FindAttributedMethods(markerTypeDefinitions.ToArray());
        foreach (var x in methods)
            decorator.Decorate(x.TypeDefinition, x.MethodDefinition, x.CustomAttribute);
    }
Beispiel #11
0
    private void DecorateAttributedByImplication(MethodDecorator.Fody.MethodDecorator decorator)
    {
        var inderectAttributes = this.ModuleDefinition.CustomAttributes
                                     .Concat(this.ModuleDefinition.Assembly.CustomAttributes)
                                     .Where(x => x.AttributeType.Name.StartsWith("IntersectMethodsMarkedByAttribute"))
                                     .Select(ToHostAttributeMapping)
                                     .Where(x=>x!=null)
                                     .ToArray();

        foreach (var inderectAttribute in inderectAttributes) {
            var methods = this.FindAttributedMethods(inderectAttribute.AttribyteTypes);
            foreach (var x in methods)
                decorator.Decorate(x.TypeDefinition, x.MethodDefinition, inderectAttribute.HostAttribute);
        }
    }