Exemple #1
0
    public override void Execute()
    {
        FindReferences(FindType);

        var collectionEquals = InjectCollectionEquals(ModuleDefinition);

        var matchingTypes = GetMatchingTypes().ToArray();

        foreach (var type in matchingTypes)
        {
            var props = type.Properties;
            foreach (var prop in props)
            {
                ModuleDefinition.ImportReference(prop.PropertyType).Resolve();
            }

            var attribute = type.CustomAttributes.Single(x => x.AttributeType.Name == attributeName);
            var typeRef   = GetGenericType(type);
            var ignoreBaseClassProperties = IsPropertySet(attribute, IgnoreBaseClassProperties);

            if (!IsPropertySet(attribute, DoNotAddEquals))
            {
                var typeCheck         = 0;
                var typeCheckProperty = attribute.Properties.SingleOrDefault(x => x.Name == "TypeCheck");
                if (typeCheckProperty.Name != null)
                {
                    typeCheck = (int)typeCheckProperty.Argument.Value;
                }

                var newEquals = InjectEqualsInternal(type, typeRef, collectionEquals, ignoreBaseClassProperties);
                InjectEqualsType(type, typeRef, newEquals);
                InjectEqualsObject(type, typeRef, newEquals, typeCheck);

                var typeInterface = IEquatableType.MakeGenericInstanceType(typeRef);
                if (type.Interfaces.All(x => x.InterfaceType.FullName != typeInterface.FullName))
                {
                    type.Interfaces.Add(new InterfaceImplementation(typeInterface));
                }
            }

            if (!IsPropertySet(attribute, DoNotAddGetHashCode))
            {
                InjectGetHashCode(type, ignoreBaseClassProperties);
            }

            if (!IsPropertySet(attribute, DoNotAddEqualityOperators))
            {
                InjectEqualityOperator(type);
                InjectInequalityOperator(type);
            }
        }

        foreach (var type in matchingTypes)
        {
            RemoveFodyAttributes(type);
        }
    }
Exemple #2
0
    public override void Execute()
    {
        if (!FindReferencesAndDetermineWhetherEqualsIsReferenced(FindType))
        {
            LogDebug("Assembly does not reference 'Equals' assembly. No work to do - exiting.");
            return;
        }

        var collectionEquals = InjectCollectionEquals(ModuleDefinition);

        var matchingTypes = GetMatchingTypes().ToArray();

        foreach (var type in matchingTypes)
        {
            var props = type.Properties;
            foreach (var prop in props)
            {
                ModuleDefinition.ImportReference(prop.PropertyType).Resolve();
            }

            var attribute = type.CustomAttributes.Single(x => x.AttributeType.Name == attributeName);
            var typeRef   = GetGenericType(type);
            var ignoreBaseClassProperties = IsPropertySet(attribute, IgnoreBaseClassProperties);

            if (!IsPropertySet(attribute, DoNotAddEquals))
            {
                var typeCheck         = 0;
                var typeCheckProperty = attribute.Properties.SingleOrDefault(x => x.Name == "TypeCheck");
                if (typeCheckProperty.Name != null)
                {
                    typeCheck = (int)typeCheckProperty.Argument.Value;
                }

                var newEquals = InjectEqualsInternal(type, typeRef, collectionEquals, ignoreBaseClassProperties);
                InjectEqualsType(type, typeRef, newEquals);
                InjectEqualsObject(type, typeRef, newEquals, typeCheck);

                var typeInterface = IEquatableType.MakeGenericInstanceType(typeRef);
                if (type.Interfaces.All(x => x.InterfaceType.FullName != typeInterface.FullName))
                {
                    type.Interfaces.Add(new InterfaceImplementation(typeInterface));
                }
            }

            if (!IsPropertySet(attribute, DoNotAddGetHashCode))
            {
                InjectGetHashCode(type, ignoreBaseClassProperties);
            }

            if (IsPropertySet(attribute, DoNotAddEqualityOperators))
            {
                WeavingInstruction.AssertNotHasWeavingInstruction(type, Operator.Equality);
                WeavingInstruction.AssertNotHasWeavingInstruction(type, Operator.Inequality);
            }
            else
            {
                ReplaceOperator(type, Operator.Equality);
                ReplaceOperator(type, Operator.Inequality);
            }
        }

        foreach (var type in matchingTypes)
        {
            RemoveFodyAttributes(type);
        }
    }