private static void GenerateParameterEntityToString(ParameterEntity entity, StructDeclaration type, PropertyDeclaration valueProperty) { var toString = type.AddMember(new MethodDeclaration("ToString")); toString.Modifiers = Modifiers.Public | Modifiers.Override; toString.ReturnType = typeof(string); if (entity.FinalType == ModelRef.NumberId) { toString.Statements = new ReturnStatement(new MethodInvokeExpression(new MemberReferenceExpression(valueProperty, "ToString"), new MemberReferenceExpression(typeof(CultureInfo), "InvariantCulture"))); } else if (entity.FinalType == ModelRef.String) { toString.Statements = new ReturnStatement(valueProperty); } else { toString.Statements = new ReturnStatement(new MethodInvokeExpression(new MemberReferenceExpression(valueProperty, "ToString"))); } if (entity.FinalType == ModelRef.Object) { toString.ReturnType = toString.ReturnType.MakeNullable(); } }
private static void GenerateParameterEntityEqualityOperators(StructDeclaration type) { var equal = type.AddMember(new OperatorDeclaration("==")); equal.Modifiers = Modifiers.Public | Modifiers.Static; equal.ReturnType = typeof(bool); equal.Arguments.Add(new MethodArgumentDeclaration(new TypeReference(type), "a")); equal.Arguments.Add(new MethodArgumentDeclaration(new TypeReference(type), "b")); equal.Statements.Add(new ReturnStatement(new TypeReferenceExpression(new TypeReference(typeof(EqualityComparer <>)).MakeGeneric(type)).CreateMemberReferenceExpression("Default").CreateInvokeMethodExpression("Equals", new ArgumentReferenceExpression("a"), new ArgumentReferenceExpression("b")))); var notEqual = type.AddMember(new OperatorDeclaration("!=")); notEqual.Modifiers = Modifiers.Public | Modifiers.Static; notEqual.ReturnType = typeof(bool); notEqual.Arguments.Add(new MethodArgumentDeclaration(new TypeReference(type), "a")); notEqual.Arguments.Add(new MethodArgumentDeclaration(new TypeReference(type), "b")); notEqual.Statements.Add(new ReturnStatement(new UnaryExpression(UnaryOperator.Not, new BinaryExpression(BinaryOperator.Equals, new ArgumentReferenceExpression("a"), new ArgumentReferenceExpression("b"))))); }
private static void GenerateParameterEntityEqualTypedMethod(StructDeclaration type, PropertyDeclaration valueProperty) { var equal = type.AddMember(new MethodDeclaration(nameof(object.Equals))); equal.Modifiers = Modifiers.Public; equal.ReturnType = typeof(bool); var objArg = equal.AddArgument("other", new TypeReference(type)); Expression returnExpression = new MethodInvokeExpression( new MemberReferenceExpression(typeof(object), "Equals"), valueProperty, new MemberReferenceExpression(objArg, valueProperty)); equal.Statements = new ReturnStatement(returnExpression); }
private static void GenerateParameterEntityGetHashCode(StructDeclaration type, PropertyDeclaration valueProperty) { var equal = type.AddMember(new MethodDeclaration(nameof(object.GetHashCode))); equal.Modifiers = Modifiers.Public | Modifiers.Override; equal.ReturnType = typeof(int); var statements = new StatementCollection { new ReturnStatement( new MethodInvokeExpression( new MemberReferenceExpression(new TypeReferenceExpression(typeof(HashCode)), nameof(HashCode.Combine)), valueProperty)), }; equal.Statements = statements; }
private static void GenerateParameterEntityEqualMethod(StructDeclaration type) { var equal = type.AddMember(new MethodDeclaration(nameof(object.Equals))); equal.Modifiers = Modifiers.Public | Modifiers.Override; equal.ReturnType = typeof(bool); var objArg = equal.AddArgument("obj", new TypeReference(typeof(object)).MakeNullable()); equal.Statements = new StatementCollection() { new ConditionStatement { Condition = new IsInstanceOfTypeExpression(objArg, type), TrueStatements = new ReturnStatement(new ThisExpression().CreateInvokeMethodExpression("Equals", new CastExpression(objArg, type))), FalseStatements = new ReturnStatement(LiteralExpression.False()), }, }; }