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 propertyName = Helpers.ToPascalCase(rawName); var fieldName = Helpers.GetFieldName(propertyName); var codegenType = CodegenTypeFactory.MakeCodegenType(rawName, schema); target.Members.AddRange(codegenType.AdditionalMembers); var propertyBackingVariable = new CodeMemberField { Type = codegenType.CodeType, Name = fieldName, Comments = { new CodeCommentStatement("<summary>", true), new CodeCommentStatement($"Backing field for {propertyName}.", 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 = propertyName, 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); }