New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ExpressionUtil / UnaryExpressionFingerprint.cs
1 #pragma warning disable 659 // overrides AddToHashCodeCombiner instead\r
2 \r
3 /* ****************************************************************************\r
4  *\r
5  * Copyright (c) Microsoft Corporation. All rights reserved.\r
6  *\r
7  * This software is subject to the Microsoft Public License (Ms-PL). \r
8  * A copy of the license can be found in the license.htm file included \r
9  * in this distribution.\r
10  *\r
11  * You must not remove this notice, or any other, from this software.\r
12  *\r
13  * ***************************************************************************/\r
14 \r
15 namespace System.Web.Mvc.ExpressionUtil {\r
16     using System;\r
17     using System.Diagnostics.CodeAnalysis;\r
18     using System.Linq.Expressions;\r
19     using System.Reflection;\r
20 \r
21     // UnaryExpression fingerprint class\r
22     // The most common appearance of a UnaryExpression is a cast or other conversion operator\r
23     [SuppressMessage("Microsoft.Usage", "CA2218:OverrideGetHashCodeOnOverridingEquals",\r
24         Justification = "Overrides AddToHashCodeCombiner() instead.")]\r
25     internal sealed class UnaryExpressionFingerprint : ExpressionFingerprint {\r
26 \r
27         private UnaryExpressionFingerprint(UnaryExpression expression)\r
28             : base(expression) {\r
29             // don't care about UnaryExpression.IsLifted / IsLiftedToNull since they're not necessary to uniquely describe the expression\r
30 \r
31             Method = expression.Method;\r
32         }\r
33 \r
34         public MethodInfo Method {\r
35             get;\r
36             private set;\r
37         }\r
38 \r
39         public ExpressionFingerprint Operand {\r
40             get;\r
41             private set;\r
42         }\r
43 \r
44         internal override void AddToHashCodeCombiner(HashCodeCombiner combiner) {\r
45             base.AddToHashCodeCombiner(combiner);\r
46 \r
47             combiner.AddObject(Method);\r
48             combiner.AddFingerprint(Operand);\r
49         }\r
50 \r
51         public static UnaryExpressionFingerprint Create(UnaryExpression expression, ParserContext parserContext) {\r
52             ExpressionFingerprint operand = Create(expression.Operand, parserContext);\r
53             if (operand == null && expression.Operand != null) {\r
54                 // couldn't convert the operand, so bail\r
55                 return null;\r
56             }\r
57 \r
58             return new UnaryExpressionFingerprint(expression) {\r
59                 Operand = operand\r
60             };\r
61         }\r
62 \r
63         public override bool Equals(object obj) {\r
64             UnaryExpressionFingerprint other = obj as UnaryExpressionFingerprint;\r
65             if (other == null) {\r
66                 return false;\r
67             }\r
68 \r
69             return (this.Method == other.Method\r
70                 && Object.Equals(this.Operand, other.Operand)\r
71                 && base.Equals(other));\r
72         }\r
73 \r
74         public override Expression ToExpression(ParserContext parserContext) {\r
75             Expression operandExpr = ToExpression(Operand, parserContext);\r
76 \r
77             // in .NET 3.5 SP1, Expression.MakeUnary() throws if NodeType is UnaryPlus, so special-case\r
78             if (NodeType == ExpressionType.UnaryPlus) {\r
79                 return Expression.UnaryPlus(operandExpr, Method);\r
80             }\r
81             else {\r
82                 return Expression.MakeUnary(NodeType, operandExpr, Type, Method);\r
83             }\r
84         }\r
85 \r
86     }\r
87 }\r