/// <summary> /// This serializes the given property on this object. /// </summary> private void SerializeNormalProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements) { using (CodeDomSerializer.TraceScope("CodeDomSerializer::" + nameof(SerializeProperty))) { CodeExpression target = SerializeToExpression(manager, value); CodeDomSerializer.TraceWarningIf(target is null, "Unable to serialize target for property {0}", property.Name); if (target != null) { CodeExpression propertyRef = new CodePropertyReferenceExpression(target, property.Name); CodeExpression serializedPropertyValue = null; // First check for a member relationship service to see if this property // is related to another member. If it is, then we will use that // relationship to construct the property assign statement. if // it isn't, then we're serialize ourselves. if (manager.GetService(typeof(MemberRelationshipService)) is MemberRelationshipService relationships) { MemberRelationship relationship = relationships[value, property]; if (relationship != MemberRelationship.Empty) { CodeExpression rhsTarget = SerializeToExpression(manager, relationship.Owner); if (rhsTarget != null) { serializedPropertyValue = new CodePropertyReferenceExpression(rhsTarget, relationship.Member.Name); } } } if (serializedPropertyValue is null) { // Serialize the value of this property into a code expression. If we can't get one, // then we won't serialize the property. // object propValue = GetPropertyValue(manager, property, value, out bool validValue); if (validValue) { ExpressionContext tree = null; if (propValue != value) { // make sure the value isn't the object or we'll end up printing // this property instead of the value. tree = new ExpressionContext(propertyRef, property.PropertyType, value); manager.Context.Push(tree); } try { serializedPropertyValue = SerializeToExpression(manager, propValue); } finally { if (tree != null) { Debug.Assert(manager.Context.Current == tree, "Context stack corrupted."); manager.Context.Pop(); } } } } if (serializedPropertyValue != null) { CodeAssignStatement assign = new CodeAssignStatement(propertyRef, serializedPropertyValue); statements.Add(assign); } } } }
/// <summary> /// This serializes the given property on this object. /// </summary> private void SerializeExtenderProperty(IDesignerSerializationManager manager, object value, PropertyDescriptor property, CodeStatementCollection statements) { AttributeCollection attributes = property.Attributes; using (CodeDomSerializer.TraceScope("PropertyMemberCodeDomSerializer::" + nameof(SerializeExtenderProperty))) { ExtenderProvidedPropertyAttribute exAttr = (ExtenderProvidedPropertyAttribute)attributes[typeof(ExtenderProvidedPropertyAttribute)]; // Extender properties are method invokes on a target "extender" object. // CodeExpression extender = SerializeToExpression(manager, exAttr.Provider); CodeExpression extended = SerializeToExpression(manager, value); CodeDomSerializer.TraceWarningIf(extender is null, "Extender object {0} could not be serialized.", manager.GetName(exAttr.Provider)); CodeDomSerializer.TraceWarningIf(extended is null, "Extended object {0} could not be serialized.", manager.GetName(value)); if (extender != null && extended != null) { CodeMethodReferenceExpression methodRef = new CodeMethodReferenceExpression(extender, "Set" + property.Name); object propValue = GetPropertyValue(manager, property, value, out bool validValue); CodeExpression serializedPropertyValue = null; // Serialize the value of this property into a code expression. If we can't get one, // then we won't serialize the property. if (validValue) { ExpressionContext tree = null; if (propValue != value) { // make sure the value isn't the object or we'll end up printing // this property instead of the value. tree = new ExpressionContext(methodRef, property.PropertyType, value); manager.Context.Push(tree); } try { serializedPropertyValue = SerializeToExpression(manager, propValue); } finally { if (tree != null) { Debug.Assert(manager.Context.Current == tree, "Context stack corrupted."); manager.Context.Pop(); } } } if (serializedPropertyValue != null) { CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression { Method = methodRef }; methodInvoke.Parameters.Add(extended); methodInvoke.Parameters.Add(serializedPropertyValue); statements.Add(methodInvoke); } } } }