コード例 #1
0
        private ClassDeclarationSyntax AddDataContractFields(ClassDeclarationSyntax classDecl, INamedTypeSymbol classInfo)
        {
            foreach (var fieldSymbol in classInfo.GetFields())
            {
                var messageBodyMemberAttr = fieldSymbol.GetAttribute("MessageBodyMemberAttribute");
                if (messageBodyMemberAttr == null)
                {
                    continue;
                }

                TypeSyntax type;
                if (fieldSymbol.GetAttribute("XmlAnyElementAttribute") == null)
                {
                    if (fieldSymbol.Type.TypeKind == TypeKind.Array)
                    {
                        type = SyntaxFactory.ParseTypeName(GetTypeName(((IArrayTypeSymbol)fieldSymbol.Type).ElementType));
                        type = SyntaxFactory.ArrayType(type).AddRankSpecifiers(SyntaxFactory.ArrayRankSpecifier());
                    }
                    else
                    {
                        type = SyntaxFactory.ParseTypeName(GetTypeName(fieldSymbol.Type));
                    }
                }
                else
                {
                    type = SyntaxFactory.ParseTypeName("XElement");
                    type = SyntaxFactory.ArrayType(type).AddRankSpecifiers(SyntaxFactory.ArrayRankSpecifier());
                }

                FieldDeclarationSyntax fieldSyntax = SyntaxTreeExtensions.FieldDeclaration(type, fieldSymbol.Name);
                //var decl = SyntaxFactory.VariableDeclarator(fieldSymbol.Name);
                //var fieldSyntax = SyntaxFactory.FieldDeclaration(SyntaxFactory.VariableDeclaration(type).WithVariables(SyntaxFactory.SeparatedList(new[] { decl })));

                var xmlElementAttr = fieldSymbol.GetAttribute("XmlElementAttribute");

                string elementName = null;
                bool   isNullable  = true;
                if (xmlElementAttr != null)
                {
                    var elementNameValue = xmlElementAttr.GetNamedArgument("ElementName").GetValueOrDefault <string>();
                    if (string.IsNullOrEmpty(elementNameValue))
                    {
                        elementName = elementNameValue;
                    }
                    if (xmlElementAttr.ConstructorArguments.Any())
                    {
                        elementName = xmlElementAttr.ConstructorArguments.First().Value as string;
                    }
                }

                var xmlArrayItemAttribute = fieldSymbol.GetAttribute("XmlArrayItemAttribute");
                if (xmlArrayItemAttribute != null)
                {
                    if (xmlArrayItemAttribute.ConstructorArguments.Any())
                    {
                        elementName = xmlArrayItemAttribute.ConstructorArguments.First().Value as string;
                    }
                    var elementNameValue = xmlArrayItemAttribute.GetNamedArgument("ElementName").GetValueOrDefault <string>();
                    if (!string.IsNullOrEmpty(elementNameValue))
                    {
                        elementName = elementNameValue;
                    }
                    isNullable = xmlArrayItemAttribute.GetNamedArgument("IsNullable").GetValueOrDefault <bool>();
                }

                var xmlElementAttrSyntax = SyntaxTreeExtensions.Attribute("XmlElementAttribute")
                                           .AddQuotedArgument("ElementName", elementName)
                                           .AddArgument("IsNullable", isNullable.ToString().ToLower());

                string ns = GetNamespace(xmlElementAttr, messageBodyMemberAttr);
                if (ns != null)
                {
                    xmlElementAttrSyntax = xmlElementAttrSyntax.AddQuotedArgument("Namespace", ns);
                }

                string dataType = GetDataType(xmlElementAttr);
                if (dataType != null)
                {
                    xmlElementAttrSyntax = xmlElementAttrSyntax.AddQuotedArgument("DataType", dataType);
                }

                int order = GetOrder(xmlElementAttr, messageBodyMemberAttr);
                xmlElementAttrSyntax = xmlElementAttrSyntax.AddArgument("Order", order);

                fieldSyntax = fieldSyntax
                              .AddAttribute(xmlElementAttrSyntax)
                              .WithModifiers(SyntaxKind.PublicKeyword); //WithModifiers(SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));

                classDecl = classDecl.AddMembers(fieldSyntax);
            }

            return(classDecl);
        }