コード例 #1
0
 private static CodeBinaryOperatorExpression CompareValueEquality(string propertyName, string otherVariableName)
 {
     return(new CodeBinaryOperatorExpression(
                ExpressionBuilder.ThisPropertyReference(propertyName),
                CodeBinaryOperatorType.ValueEquality,
                OtherPropertyReference(propertyName, otherVariableName)));
 }
コード例 #2
0
        private static CodeBinaryOperatorExpression ComparePropertyValueEqualityExpression(ArrayType propertyType,
                                                                                           String propertyName,
                                                                                           String otherVariableName)
        {
            // TODO: this should probably be a warning in the model
            if (propertyType.RankSpecifiers.Ranks.Count() > 1)
            {
                throw new InvalidOperationException(
                          "Cannot generate equality for Array Types with more than one rank-specifier.");
            }
            var rankSpecifier = propertyType.RankSpecifiers.Ranks.Single();

            if (rankSpecifier.Dimensions > 1)
            {
                throw new InvalidOperationException(
                          "Cannot generate equality for Array Type with more than one dimension");
            }

            // (this.Property.Length == other.Property.Length)
            // && Enumerable.Zip(a, b, (a, b) => Object.Equals(a, b)).All(areEqual => areEqual);

            var thisPropertyReference  = ExpressionBuilder.ThisPropertyReference(propertyName);
            var otherPropertyReference = OtherPropertyReference(propertyName, otherVariableName);

            var bothNull = new CodeBinaryOperatorExpression(CompareToNull(thisPropertyReference),
                                                            CodeBinaryOperatorType.BooleanAnd,
                                                            CompareToNull(otherPropertyReference));

            var bothNotNull = new CodeBinaryOperatorExpression(
                ExpressionBuilder.Negate(CompareToNull(thisPropertyReference)),
                CodeBinaryOperatorType.BooleanAnd,
                ExpressionBuilder.Negate(CompareToNull(otherPropertyReference)));

            var sameArrayLength = new CodeBinaryOperatorExpression(
                new CodePropertyReferenceExpression(thisPropertyReference, "Length"),
                CodeBinaryOperatorType.ValueEquality,
                new CodePropertyReferenceExpression(otherPropertyReference, "Length"));

            var zipExpression = new CodeMethodInvokeExpression(
                new CodeMethodReferenceExpression(
                    new CodeTypeReferenceExpression(typeof(Enumerable)),
                    "Zip"),
                thisPropertyReference,
                otherPropertyReference,
                new CodeSnippetExpression("(a, b) => Object.Equals(a,b)"));

            var zipPairwiseEquality =
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression(typeof(Enumerable)),
                        "All"),
                    zipExpression,
                    new CodeSnippetExpression("areEqual => areEqual")
                    );

            return(new CodeBinaryOperatorExpression(
                       bothNull,
                       CodeBinaryOperatorType.BooleanOr,
                       new CodeBinaryOperatorExpression(
                           bothNotNull,
                           CodeBinaryOperatorType.BooleanAnd,
                           new CodeBinaryOperatorExpression(sameArrayLength,
                                                            CodeBinaryOperatorType.BooleanAnd, zipPairwiseEquality))));
        }
コード例 #3
0
 private static CodeExpression CompareObjectEquality(string propertyName, string otherVariableName)
 {
     return(ExpressionBuilder.ObjectEquals(ExpressionBuilder.ThisPropertyReference(propertyName),
                                           OtherPropertyReference(propertyName, otherVariableName)));
 }