Guest User

equals generator

a guest
Mar 12th, 2015
218
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private void AddEqualsMethod(ref CodeTypeDeclaration type, TinyType tinyType)
  2. {
  3.     // Create method
  4.     var method = new CodeMemberMethod
  5.     {
  6.         Name = "Equals",
  7.         Attributes = MemberAttributes.Public | MemberAttributes.Override,
  8.         ReturnType = new CodeTypeReference(typeof (bool)),
  9.     };
  10.     method.Parameters.Add(new CodeParameterDeclarationExpression(typeof (object), "o"));
  11.    
  12.     // Reference to the null value
  13.     var nullValue = new CodeDefaultValueExpression(new CodeTypeReference(typeof (object)));
  14.    
  15.     // Check if the argument is null
  16.     var isArgumentNull = new CodeBinaryOperatorExpression
  17.     {
  18.         Left = new CodeArgumentReferenceExpression("o"),
  19.         Operator = CodeBinaryOperatorType.IdentityEquality,
  20.         Right = nullValue
  21.     };
  22.    
  23.     // null condition
  24.     var nullCondition = new CodeConditionStatement(isArgumentNull);
  25.     nullCondition.TrueStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(false)));
  26.    
  27.     // Check for type equality
  28.     var isTypeEqual = new CodeBinaryOperatorExpression
  29.     {
  30.         Left = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "GetType"),
  31.         Operator = CodeBinaryOperatorType.IdentityEquality,
  32.         Right = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("o"), "GetType")
  33.     };
  34.    
  35.     var equalityCondition = new CodeConditionStatement {Condition = isTypeEqual};
  36.    
  37.     // Cast Object to the correct type
  38.     var tempVar = new CodeVariableDeclarationStatement(tinyType.Name, "tmp");
  39.     var cast = new CodeCastExpression
  40.     {
  41.         TargetType = new CodeTypeReference(tinyType.Name),
  42.         Expression = new CodeArgumentReferenceExpression("o")
  43.     };
  44.     tempVar.InitExpression = cast;
  45.    
  46.     // Add return check on the .Value property
  47.     var trueCondition = new CodeBinaryOperatorExpression
  48.     {
  49.         Left = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), _parameters.DefaultFieldName),
  50.         Operator = CodeBinaryOperatorType.IdentityEquality,
  51.         Right = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("tmp"), _parameters.DefaultFieldName)
  52.     };
  53.    
  54.     // Add to return condition
  55.     var trueReturn = new CodeMethodReturnStatement(trueCondition);
  56.    
  57.     // Add elements to true-if-body
  58.     equalityCondition.TrueStatements.Add(tempVar);
  59.     equalityCondition.TrueStatements.Add(trueReturn);
  60.    
  61.     // Add checks to method
  62.     method.Statements.Add(nullCondition);
  63.     method.Statements.Add(equalityCondition);
  64.    
  65.     // Fallthrough return statement
  66.     method.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(false)));
  67.     type.Members.Add(method);
  68. }
RAW Paste Data