コード例 #1
0
        private EnumDeclarationSyntax AddEnum(INamedTypeSymbol enumInfo)
        {
            var enumDecl = SyntaxFactory
                           .EnumDeclaration(enumInfo.Name)
                           .WithModifiers(SyntaxKind.PublicKeyword);

            foreach (var member in enumInfo.GetFields())
            {
                var memberSyntax = SyntaxFactory.EnumMemberDeclaration(member.Name);

                var xmlEnumAttr = member.GetAttribute("XmlEnumAttribute");
                if (xmlEnumAttr != null)
                {
                    if (xmlEnumAttr.ConstructorArguments.Any())
                    {
                        var attribute = SyntaxTreeExtensions.Attribute("XmlEnumAttribute")
                                        .AddQuotedArgument(xmlEnumAttr.ConstructorArguments.First().Value);
                        memberSyntax = memberSyntax.AddAttribute(attribute);
                    }
                }

                enumDecl = enumDecl.AddMembers(memberSyntax);
            }

            return(enumDecl);
        }
コード例 #2
0
        private ClassDeclarationSyntax AddDataContractProperties(ClassDeclarationSyntax classDecl, INamedTypeSymbol classInfo)
        {
            foreach (var propertySymbol in classInfo.GetProperties()
                     .Where(prop => prop.GetAttribute("XmlAnyAttributeAttribute") == null && prop.GetAttribute("XmlIgnoreAttribute") == null))
            {
                if (propertySymbol.Type.Name == "XmlNode" || propertySymbol.Type.Name == "ExtensionDataObject")
                {
                    continue;
                }

                var name = propertySymbol.Name == "fixed" ? "@fixed" : propertySymbol.Name;
                var list = SyntaxFactory.AccessorList()
                           .AddAccessors(SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)))
                           .AddAccessors(SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

                var propertySyntax = SyntaxFactory.PropertyDeclaration(GetPropertyType(propertySymbol.Type), name)
                                     .WithModifiers(SyntaxKind.PublicKeyword)
                                     .WithAccessorList(list);


                var xmlElementAttrs = propertySymbol.GetAttributes("XmlElementAttribute");
                foreach (var xmlElementAttr in xmlElementAttrs)
                {
                    var xmlElementAttrSyntax = SyntaxTreeExtensions.Attribute("XmlElementAttribute");

                    var elementName = xmlElementAttr.GetNamedArgument("ElementName").GetValueOrDefault <string>();
                    if (!string.IsNullOrEmpty(elementName))
                    {
                        xmlElementAttrSyntax = xmlElementAttrSyntax.AddQuotedArgument("ElementName", elementName);
                    }
                    if (xmlElementAttr.ConstructorArguments.Any())
                    {
                        elementName          = xmlElementAttr.ConstructorArguments.First().Value as string;
                        xmlElementAttrSyntax = xmlElementAttrSyntax.AddQuotedArgument("ElementName", elementName);
                    }

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

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

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

                    //propertySyntax = propertySyntax.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(xmlElementAttrSyntax)); //+-
                    propertySyntax = propertySyntax.AddAttribute(xmlElementAttrSyntax);                     //+-
                }

                var xmlAnyElementAttr = propertySymbol.GetAttribute("XmlAnyElementAttribute");
                if (xmlAnyElementAttr != null)
                {
                    var xmlAnyElementAttrSyntax = SyntaxTreeExtensions.Attribute("XmlAnyElementAttribute");

                    var ns = xmlAnyElementAttr.GetNamedArgument("Namespace").GetValueOrDefault <string>();
                    if (ns != null)
                    {
                        xmlAnyElementAttrSyntax = xmlAnyElementAttrSyntax.AddQuotedArgument("Namespace", ns);
                    }

                    var order = xmlAnyElementAttr.GetNamedArgument("Order").GetValueOrDefault <int>();
                    xmlAnyElementAttrSyntax = xmlAnyElementAttrSyntax.AddArgument("Order", order);

                    propertySyntax = propertySyntax.AddAttribute(xmlAnyElementAttrSyntax);
                }

                var xmlAttributeAttr = propertySymbol.GetAttribute("XmlAttributeAttribute");
                if (xmlAttributeAttr != null)
                {
                    var xmlAttributeAttrSyntax = SyntaxTreeExtensions.Attribute("XmlAttributeAttribute");

                    var ns = xmlAttributeAttr.GetNamedArgument("Namespace").GetValueOrDefault <string>();
                    if (ns != null)
                    {
                        xmlAttributeAttrSyntax = xmlAttributeAttrSyntax.AddQuotedArgument("Namespace", ns);
                    }

                    var dataType = xmlAttributeAttr.GetNamedArgument("DataType").GetValueOrDefault <string>();
                    if (!string.IsNullOrEmpty(dataType))
                    {
                        xmlAttributeAttrSyntax = xmlAttributeAttrSyntax.AddQuotedArgument("DataType", dataType);
                    }

                    var form = xmlAttributeAttr.GetNamedArgument("Form").GetValueOrDefault <int>();
                    if (form != (int)XmlSchemaForm.None)
                    {
                        var expression = SyntaxFactory.ParseExpression(string.Format("{0} = XmlSchemaForm.{1}", "Form", Enum.GetName(typeof(XmlSchemaForm), form)));
                        var arg        = SyntaxFactory.AttributeArgument(expression);
                        xmlAttributeAttrSyntax = xmlAttributeAttrSyntax.AddArgumentListArguments(arg);
                    }

                    propertySyntax = propertySyntax.AddAttribute(xmlAttributeAttrSyntax);
                }

                var xmlTextAttr = propertySymbol.GetAttribute("XmlTextAttribute");
                if (xmlTextAttr != null)
                {
                    var xmlTextAttrSyntax = SyntaxTreeExtensions.Attribute("XmlTextAttribute");

                    propertySyntax = propertySyntax.AddAttribute(xmlTextAttrSyntax);
                }

                classDecl = classDecl.AddMembers(propertySyntax);
            }

            return(classDecl);
        }
コード例 #3
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);
        }
コード例 #4
0
        private ClassDeclarationSyntax AddDataContract(INamedTypeSymbol classInfo)
        {
            var classDecl = SyntaxFactory
                            .ClassDeclaration(classInfo.Name)
                            .WithModifiers(SyntaxKind.PublicKeyword);

            if (classInfo.BaseType.Name != "Object")
            {
                //var baseTypeList = SyntaxFactory.BaseList().AddTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(classInfo.BaseType.ToString())));
                classDecl = classDecl.WithBaseList(classInfo.BaseType.ToString());
            }

            foreach (var customAttribute in classInfo.GetAttributes("XmlIncludeAttribute"))
            {
                var attr = SyntaxTreeExtensions.Attribute("XmlIncludeAttribute")
                           .AddArgument(string.Format("typeof({0})", customAttribute.ConstructorArguments.First().Value));

                classDecl = classDecl.AddAttribute(attr);
            }

            var xmlTypeAttr         = classInfo.GetAttribute("XmlTypeAttribute");         //classInfo.GetAttributes().FirstOrDefault(attr => attr.AttributeClass.Name == "XmlTypeAttribute");
            var messageContractAttr = classInfo.GetAttribute("MessageContractAttribute"); //.GetAttributes().FirstOrDefault(attr => attr.AttributeClass.Name == "MessageContractAttribute");

            bool   addXmlRoot         = false;
            string xmlRootElementName = null;
            string xmlRootNamespace   = null;

            if (xmlTypeAttr != null)
            {
                addXmlRoot       = true;
                xmlRootNamespace = xmlTypeAttr.GetNamedArgument("Namespace").GetValueOrDefault <string>();
            }
            else
            {
                if (messageContractAttr != null)
                {
                    addXmlRoot         = true;
                    xmlRootElementName = messageContractAttr.GetNamedArgument("WrapperName").GetValueOrDefault <string>();      //.NamedArguments.First(item => item.Key == "WrapperName").Value.Value as string;
                    xmlRootNamespace   = messageContractAttr.GetNamedArgument("WrapperNamespace").GetValueOrDefault <string>(); //.NamedArguments.First(item => item.Key == "WrapperNamespace").Value.Value as string;
                }
            }

            var bodyMember = classInfo.GetFields().FirstOrDefault(m => m.GetAttribute("MessageBodyMemberAttribute") != null);

            if (bodyMember != null)
            {
                xmlRootElementName = bodyMember.Name;
                var bodyAttr = bodyMember.GetAttribute("MessageBodyMemberAttribute");
                xmlRootNamespace = bodyAttr.GetNamedArgument("Namespace").GetValueOrDefault <string>();
            }

            if (addXmlRoot)
            {
                var attr = SyntaxTreeExtensions.Attribute("XmlRootAttribute")
                           .AddQuotedArgument("ElementName", xmlRootElementName)
                           .AddQuotedArgument("Namespace", xmlRootNamespace);

                //classDecl = classDecl.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(attr));//+-
                classDecl = classDecl.AddAttribute(attr);
            }

            classDecl = AddDataContractFields(classDecl, classInfo);
            classDecl = AddDataContractProperties(classDecl, classInfo);

            return(classDecl);
        }