private CodeMemberProperty GenerateCount(IEnumerable <IReference> implementingReferences, ITransformationContext context) { var count = new CodeMemberProperty() { Name = "Count", Attributes = MemberAttributes.Public | MemberAttributes.Override, Type = new CodeTypeReference(typeof(int)), HasGet = true, HasSet = false }; count.GetStatements.Add(new CodeVariableDeclarationStatement(typeof(int), "count", new CodePrimitiveExpression(0))); var countRef = new CodeVariableReferenceExpression("count"); foreach (var reference in implementingReferences) { var propertyRef = GetPropertyReference(reference, context); if (reference.UpperBound == 1) { var ifNull = new CodeConditionStatement(); ifNull.Condition = new CodeBinaryOperatorExpression(propertyRef, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)); ifNull.TrueStatements.Add(new CodeAssignStatement(countRef, new CodeBinaryOperatorExpression(countRef, CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)))); count.GetStatements.Add(ifNull); } else { var propertyCount = new CodePropertyReferenceExpression(propertyRef, "Count"); count.GetStatements.Add(new CodeAssignStatement(countRef, new CodeBinaryOperatorExpression(countRef, CodeBinaryOperatorType.Add, propertyCount))); } } count.GetStatements.Add(new CodeMethodReturnStatement(countRef)); count.WriteDocumentation("Gets the amount of elements contained in this collection"); return(count); }
public override void Transform(ITypedElement feature, CodeTypeDeclaration generatedType, ITransformationContext context) { generatedType.Name = feature.Name.ToPascalCase() + "Proxy"; generatedType.Attributes = MemberAttributes.Private | MemberAttributes.Final; generatedType.TypeAttributes = System.Reflection.TypeAttributes.NestedPrivate | System.Reflection.TypeAttributes.Sealed; generatedType.WriteDocumentation(string.Format("Represents a proxy to represent an incremental access to the {0} property", feature.Name)); var type = CreateReference(feature.Type, feature is IReference, context); var t = Transformation as Meta2ClassesTransformation; if ((t == null || t.IsValueType(feature.Type)) && feature.LowerBound == 0 && feature.UpperBound == 1) { type = new CodeTypeReference(typeof(System.Nullable <>).Name, type); } var declaringType = context.Trace.ResolveIn(Rule <Type2Type>(), feature.Parent as IType).GetReferenceForType(); generatedType.BaseTypes.Add(new CodeTypeReference("ModelPropertyChange", declaringType, type)); var modelElementRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ModelElement"); var property = context.Trace.ResolveIn(Rule <Feature2Property>(), feature); var propertyRef = new CodePropertyReferenceExpression(modelElementRef, property.Name); var propertyChanged = new CodeEventReferenceExpression(modelElementRef, property.Name + "Changed"); var value = new CodeMemberProperty() { Name = "Value", Attributes = MemberAttributes.Public | MemberAttributes.Override, Type = type }; value.WriteDocumentation("Gets or sets the value of this expression"); value.GetStatements.Add(new CodeMethodReturnStatement(propertyRef)); value.SetStatements.Add(new CodeAssignStatement(propertyRef, new CodePropertySetValueReferenceExpression())); generatedType.Members.Add(value); var constructor = new CodeConstructor() { Attributes = MemberAttributes.Public }; constructor.WriteDocumentation("Creates a new observable property access proxy", null, new Dictionary <string, string>() { { "modelElement", "The model instance element for which to create the property access proxy" } }); constructor.Parameters.Add(new CodeParameterDeclarationExpression(declaringType, "modelElement")); constructor.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("modelElement")); constructor.BaseConstructorArgs.Add(new CodePrimitiveExpression(feature.Name)); generatedType.Members.Add(constructor); }