CodeTypeMember[] GenerateProperty(PropertyData property, ClassData classData)
        {
            // 

            CodeTypeReference propertyCodeType = null;

            if (!GetCodeTypeReferenceFromXamlType(property.Type, classData, out propertyCodeType))
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.TaskCannotResolvePropertyType(
                    XamlBuildTaskServices.GetFullTypeName(property.Type), property.Name)), classData.FileName);
            }

            if (!this.codeDomProvider.IsValidIdentifier(property.Name))
            {
                throw FxTrace.Exception.AsError(
                    new InvalidOperationException(SR.InvalidIdentifiers(property.Name)),
                    classData.FileName);
            }

            //     private <%= property.Type %> _<%= property.Name %>;
            //
            CodeMemberField fieldMember = new CodeMemberField()
                {
                    Attributes = MemberAttributes.Private,
                    Name = "_" + property.Name,
                    Type = propertyCodeType
                };

            //    public <%= property.Type %> <%= property.Name %> {
            //       get { return this._<%= property.Name %>; }
            //       set { this._<%= property.Name %> = value; }
            //    }
            //
            CodeMemberProperty propertyMember = new CodeMemberProperty()
                {
                    Attributes = MemberAttributes.Final,
                    Name = property.Name,
                    Type = propertyCodeType,
                    GetStatements =
                    {
                        new CodeMethodReturnStatement(CodeThis.Field(fieldMember.Name))
                    },
                    SetStatements =
                    {
                        new CodeAssignStatement()
                        {
                            Left = CodeThis.Field(fieldMember.Name),
                            Right = new CodeVariableReferenceExpression("value")
                        }
                    }
                };
            propertyMember.Attributes |= GetMemberAttributes(property.Visibility);

            if (property.Attributes != null && property.Attributes.Count > 0)
            {
                CodeAttributeDeclarationCollection attributeCollection = GetAttributeDeclarations(property.Attributes, classData);
                if (attributeCollection != null && attributeCollection.Count > 0)
                {
                    propertyMember.CustomAttributes.AddRange(attributeCollection);
                }
            }

            return new CodeTypeMember[] { fieldMember, propertyMember };
        }
예제 #2
0
        PropertyData LoadProperty(XamlReader xamlReader)
        {
            if (xamlReader == null)
            {
                throw FxTrace.Exception.ArgumentNull("xamlReader");
            }

            PropertyData property = new PropertyData();
            while (xamlReader.Read())
            {
                if (xamlReader.NodeType == XamlNodeType.StartMember)
                {
                    XamlMember member = xamlReader.Member;
                    switch (member.Name)
                    {
                        case "Name":
                            property.Name = ReadValueAsString(xamlReader.ReadSubtree());
                            break;
                        case "Type":
                            property.Type = ReadPropertyType(xamlReader.ReadSubtree());
                            break;
                        case "Attributes":
                            foreach (AttributeData attribute in ReadAttributesCollection(xamlReader.ReadSubtree()))
                            {
                                property.Attributes.Add(attribute);
                            }
                            break;
                        case "Modifier":
                            string propertyModifier = ReadValueAsString(xamlReader.ReadSubtree());
                            property.Visibility = XamlBuildTaskServices.GetMemberVisibility(propertyModifier);
                            break;
                        default:
                            // Ignore AttachedProperties on property
                            if (!member.IsAttachable)
                            {
                                throw FxTrace.Exception.AsError(LogInvalidOperationException(xamlReader, SR.UnknownPropertyMember(member.Name)));
                            }                            
                            break;
                    }
                }
            }
            if (string.IsNullOrEmpty(property.Name))
            {
                throw FxTrace.Exception.AsError(LogInvalidOperationException(xamlReader, SR.PropertyNameRequired));
            }
            if (property.Type == null)
            {
                throw FxTrace.Exception.AsError(LogInvalidOperationException(xamlReader, SR.PropertyTypeRequired(property.Name)));
            }
            return property;
        }