Ejemplo n.º 1
0
        public static CodeTypeReference GetCodegenType(CodeTypeDeclaration target, Schema schema, string name, out CodeAttributeDeclarationCollection attributes, out CodeExpression defaultValue)
        {
            var codegenType = CodegenTypeFactory.MakeCodegenType(name, schema);

            attributes   = codegenType.Attributes;
            defaultValue = codegenType.DefaultValue;
            target.Members.AddRange(codegenType.AdditionalMembers);

            return(codegenType.CodeType);
        }
        private void AddProperty(CodeTypeDeclaration target, string rawName, Schema schema)
        {
            var name        = Helpers.ParsePropertyName(rawName);
            var fieldName   = "m_" + name.Substring(0, 1).ToLower() + name.Substring(1);
            var codegenType = CodegenTypeFactory.MakeCodegenType(rawName, schema);

            codegenType = SupportExtrasAndExtensions(name, codegenType);
            target.Members.AddRange(codegenType.AdditionalMembers);

            var propertyBackingVariable = new CodeMemberField
            {
                Type           = codegenType.CodeType,
                Name           = fieldName,
                Comments       = { new CodeCommentStatement("<summary>", true), new CodeCommentStatement($"Backing field for {name}.", true), new CodeCommentStatement("</summary>", true) },
                InitExpression = codegenType.DefaultValue
            };

            target.Members.Add(propertyBackingVariable);

            var setStatements = codegenType.SetStatements ?? new CodeStatementCollection();

            setStatements.Add(new CodeAssignStatement()
            {
                Left = new CodeFieldReferenceExpression
                {
                    FieldName    = fieldName,
                    TargetObject = new CodeThisReferenceExpression()
                },
                Right = new CodePropertySetValueReferenceExpression()
            });

            var property = new CodeMemberProperty
            {
                Type             = codegenType.CodeType,
                Name             = name,
                Attributes       = MemberAttributes.Public | MemberAttributes.Final,
                HasGet           = true,
                GetStatements    = { new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)) },
                HasSet           = true,
                Comments         = { new CodeCommentStatement("<summary>", true), new CodeCommentStatement(schema.Description, true), new CodeCommentStatement("</summary>", true) },
                CustomAttributes = codegenType.Attributes
            };

            property.SetStatements.AddRange(setStatements);

            target.Members.Add(property);
        }