Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void AddEqualsMethod(ref CodeTypeDeclaration type, TinyType tinyType)
- {
- // Create method
- var method = new CodeMemberMethod
- {
- Name = "Equals",
- Attributes = MemberAttributes.Public | MemberAttributes.Override,
- ReturnType = new CodeTypeReference(typeof (bool)),
- };
- method.Parameters.Add(new CodeParameterDeclarationExpression(typeof (object), "o"));
- // Reference to the null value
- var nullValue = new CodeDefaultValueExpression(new CodeTypeReference(typeof (object)));
- // Check if the argument is null
- var isArgumentNull = new CodeBinaryOperatorExpression
- {
- Left = new CodeArgumentReferenceExpression("o"),
- Operator = CodeBinaryOperatorType.IdentityEquality,
- Right = nullValue
- };
- // null condition
- var nullCondition = new CodeConditionStatement(isArgumentNull);
- nullCondition.TrueStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(false)));
- // Check for type equality
- var isTypeEqual = new CodeBinaryOperatorExpression
- {
- Left = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "GetType"),
- Operator = CodeBinaryOperatorType.IdentityEquality,
- Right = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("o"), "GetType")
- };
- var equalityCondition = new CodeConditionStatement {Condition = isTypeEqual};
- // Cast Object to the correct type
- var tempVar = new CodeVariableDeclarationStatement(tinyType.Name, "tmp");
- var cast = new CodeCastExpression
- {
- TargetType = new CodeTypeReference(tinyType.Name),
- Expression = new CodeArgumentReferenceExpression("o")
- };
- tempVar.InitExpression = cast;
- // Add return check on the .Value property
- var trueCondition = new CodeBinaryOperatorExpression
- {
- Left = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), _parameters.DefaultFieldName),
- Operator = CodeBinaryOperatorType.IdentityEquality,
- Right = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("tmp"), _parameters.DefaultFieldName)
- };
- // Add to return condition
- var trueReturn = new CodeMethodReturnStatement(trueCondition);
- // Add elements to true-if-body
- equalityCondition.TrueStatements.Add(tempVar);
- equalityCondition.TrueStatements.Add(trueReturn);
- // Add checks to method
- method.Statements.Add(nullCondition);
- method.Statements.Add(equalityCondition);
- // Fallthrough return statement
- method.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(false)));
- type.Members.Add(method);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement