Exemple #1
0
        private DifferenceType CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            DifferenceType difference = DifferenceType.Unchanged;
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);
            AttributeComparer attributeComparer = new AttributeComparer();

            attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer));
            attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer));

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    // Allow for additions
                    differences.Add(new Difference("AddedAttribute",
                                                   $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Implementation} but not the {Contract}."));

                    break;
                }

                case DifferenceType.Changed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    string contractKey       = attributeComparer.GetKey(group[0].Attributes.First());
                    string implementationKey = attributeComparer.GetKey(group[1].Attributes.First());

                    differences.AddIncompatibleDifference("CannotChangeAttribute",
                                                          $"Attribute '{type.FullName()}' on '{target.FullName()}' changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}.");

                    difference = DifferenceType.Changed;
                    break;
                }

                case DifferenceType.Removed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Contract} but not the {Implementation}.");


                    // removals of an attribute are considered a "change" of the type
                    difference = DifferenceType.Changed;
                    break;
                }
                }
            }
            return(difference);
        }
        private bool CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes, IReference member = null)
        {
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);
            AttributeComparer attributeComparer = new AttributeComparer();

            attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer));
            attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer));

            string errString = $"'{target.FullName()}'";

            if (target is IParameterDefinition || target is IGenericParameter)
            {
                errString  = target is IGenericParameter ? "generic param" : "parameter";
                errString += $" '{target.FullName()}' on member '{member?.FullName()}'";
            }

            bool changed = false;

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    // Allow for additions
                    differences.Add(new Difference("AddedAttribute",
                                                   $"Attribute '{type.FullName()}' exists on {errString} in the {Implementation} but not the {Contract}."));

                    changed = true;
                    break;
                }

                case DifferenceType.Changed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    string contractKey       = attributeComparer.GetKey(group[0].Attributes.First());
                    string implementationKey = attributeComparer.GetKey(group[1].Attributes.First());

                    differences.AddIncompatibleDifference("CannotChangeAttribute",
                                                          $"Attribute '{type.FullName()}' on {errString} changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}.");

                    changed = true;
                    break;
                }

                case DifferenceType.Removed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          $"Attribute '{type.FullName()}' exists on {errString} in the {Contract} but not the {Implementation}.");


                    // removals of an attribute are considered a "change" of the type
                    changed = true;
                    break;
                }
                }
            }
            return(changed);
        }