public override void VisitPropertyInitializerExpression(PropertyInitializerExpression node) { WritePropertyName(node.Property); }
public override void VisitPropertyInitializerExpression(PropertyInitializerExpression node) { WriteToken("."); base.VisitPropertyInitializerExpression(node); }
// Person person = new Person { Name = "John", Age = 20 }; // // == // // Person person = new Person(); // person.Name = "John"; // person.Age = 20; protected override bool TryMatchInternal(StatementCollection statements, int startIndex, out Statement result, out int replacedStatementsCount) { result = null; replacedStatementsCount = 0; ObjectCreationExpression objectCreation; Expression assignee; if (!TryGetObjectCreation(statements, startIndex, out objectCreation, out assignee)) { return false; } ExpressionCollection inlinedExpressions = new ExpressionCollection(); HashSet<string> visitedPropertyNames = new HashSet<string>(); if (objectCreation.Initializer != null) { if (objectCreation.Initializer.InitializerType != InitializerType.ObjectInitializer) { return false; } foreach (var item in objectCreation.Initializer.Expressions) { string name = GetName((item as BinaryExpression).Left); visitedPropertyNames.Add(name); } } for (int i = startIndex + 1; i < statements.Count; i++) { Expression expression; if (!TryGetNextExpression(statements[i], out expression)) { break; } BinaryExpression assignment = expression as BinaryExpression; if (!IsObjectPropertyOrFieldAssignment(assignment, assignee)) { break; } Expression initializer = null; if (assignment.Left.CodeNodeType == CodeNodeType.PropertyReferenceExpression) { PropertyDefinition property = (assignment.Left as PropertyReferenceExpression).Property; if (!Visit(property.Name, visitedPropertyNames)) { break; } initializer = new PropertyInitializerExpression(property, property.PropertyType, assignment.Right.UnderlyingSameMethodInstructions); } else if (assignment.Left.CodeNodeType == CodeNodeType.FieldReferenceExpression) { FieldDefinition field = (assignment.Left as FieldReferenceExpression).Field.Resolve(); if (!Visit(field.Name, visitedPropertyNames)) { break; } initializer = new FieldInitializerExpression(field, field.FieldType, assignment.Right.UnderlyingSameMethodInstructions); } var inlinedAssignment = new BinaryExpression(BinaryOperator.Assign, initializer, assignment.Right.Clone(), this.typeSystem, null); inlinedExpressions.Add(inlinedAssignment); } if (inlinedExpressions.Count == 0) { return false; } if (objectCreation.Initializer == null) { var initializer = new InitializerExpression(inlinedExpressions, InitializerType.ObjectInitializer); initializer.IsMultiLine = true; objectCreation.Initializer = initializer; } else { foreach (var item in inlinedExpressions) { objectCreation.Initializer.Expressions.Add(item); } } result = statements[startIndex]; replacedStatementsCount = inlinedExpressions.Count + 1; return true; }
public virtual void VisitPropertyInitializerExpression(PropertyInitializerExpression node) { }