public static CodeConstructor CreateXRootFunctionalConstructor(string typeName)
        {
            CodeConstructor constructor = CodeDomHelper.CreateConstructor(MemberAttributes.Public);

            constructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeName), "root"));
            CodeStatementCollection      statements = constructor.Statements;
            CodeFieldReferenceExpression codeFieldReferenceExpression = new CodeFieldReferenceExpression(CodeDomHelper.This(), "doc");

            CodeExpression[] codePropertyReferenceExpression = new CodeExpression[] { new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("root"), "Untyped") };
            statements.Add(new CodeAssignStatement(codeFieldReferenceExpression, new CodeObjectCreateExpression("XDocument", codePropertyReferenceExpression)));
            constructor.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(CodeDomHelper.This(), "rootObject"), new CodeVariableReferenceExpression("root")));
            return(constructor);
        }
Exemple #2
0
        internal override CodeConstructor CreateFunctionalConstructor(List <ClrAnnotation> annotations)
        {
            string            parameterName = "content";
            CodeConstructor   constructor   = CodeDomHelper.CreateConstructor(MemberAttributes.Public);
            CodeTypeReference returnType    = null;

            if (!this.isSchemaList)
            {
                returnType = new CodeTypeReference(this.simpleTypeName);
            }
            else
            {
                CodeTypeReference[] codeTypeReference = new CodeTypeReference[] { new CodeTypeReference(this.simpleTypeName) };
                returnType = new CodeTypeReference("IList", codeTypeReference);
            }
            constructor.Parameters.Add(new CodeParameterDeclarationExpression(returnType, parameterName));
            constructor.Statements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(CodeDomHelper.This(), "TypedValue"), new CodeVariableReferenceExpression(parameterName)));
            TypeBuilder.ApplyAnnotations(constructor, annotations, null);
            this.decl.Members.Add(constructor);
            return(constructor);
        }
        public static CodeTypeMember CreateStaticMethod(string methodName, string typeT, string typeT1, string parameterName, string parameterType, bool useAutoTyping)
        {
            CodeExpression[] codeExpressionArray;
            CodeMemberMethod staticMethod = new CodeMemberMethod()
            {
                Name       = methodName,
                Attributes = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public,
                ReturnType = new CodeTypeReference(typeT)
            };

            staticMethod.Parameters.Add(CodeDomHelper.CreateParameter(parameterName, parameterType));
            CodeExpression parameterExp = new CodeVariableReferenceExpression(parameterName);

            if (!useAutoTyping)
            {
                CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression("XTypedServices");
                string str = string.Concat(methodName, "<", CodeDomHelper.GetInnerType(typeT, typeT1), ">");
                codeExpressionArray = new CodeExpression[] { parameterExp };
                CodeMethodInvokeExpression methodCall = CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression, str, codeExpressionArray);
                if (typeT1 != null)
                {
                    methodCall.Parameters.Add(CodeDomHelper.SingletonTypeManager());
                }
                staticMethod.Statements.Add(new CodeMethodReturnStatement(methodCall));
            }
            else
            {
                CodeStatementCollection     statements = staticMethod.Statements;
                CodeTypeReference           returnType = staticMethod.ReturnType;
                CodeTypeReferenceExpression codeTypeReferenceExpression1 = new CodeTypeReferenceExpression("XTypedServices");
                codeExpressionArray = new CodeExpression[2];
                CodeTypeReferenceExpression codeTypeReferenceExpression2 = new CodeTypeReferenceExpression("XElement");
                CodeExpression[]            codeExpressionArray1         = new CodeExpression[] { parameterExp };
                codeExpressionArray[0] = CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression2, methodName, codeExpressionArray1);
                codeExpressionArray[1] = CodeDomHelper.SingletonTypeManager();
                statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(returnType, CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression1, "ToXTypedElement", codeExpressionArray))));
            }
            return(staticMethod);
        }
        private CodeMemberProperty CreateUntypedProperty(CodeFieldReferenceExpression fieldRef)
        {
            CodeMemberProperty xElementProperty = CodeDomHelper.CreateProperty(new CodeTypeReference("XElement"), true);

            xElementProperty.Name       = "Untyped";
            xElementProperty.Attributes = MemberAttributes.Override | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public;
            CodePropertyReferenceExpression baseUntyped = new CodePropertyReferenceExpression(new CodeBaseReferenceExpression(), "Untyped");

            xElementProperty.GetStatements.Add(new CodeMethodReturnStatement(baseUntyped));
            xElementProperty.SetStatements.Add(new CodeAssignStatement(baseUntyped, CodeDomHelper.SetValue()));
            if (!this.clrTypeInfo.IsSubstitutionHead)
            {
                xElementProperty.SetStatements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(fieldRef, "Untyped"), CodeDomHelper.SetValue()));
            }
            else
            {
                CodeStatementCollection      setStatements = xElementProperty.SetStatements;
                CodeBinaryOperatorExpression codeBinaryOperatorExpression = new CodeBinaryOperatorExpression(fieldRef, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
                CodeStatement[] codeAssignStatement = new CodeStatement[] { new CodeAssignStatement(new CodePropertyReferenceExpression(fieldRef, "Untyped"), CodeDomHelper.SetValue()) };
                setStatements.Add(new CodeConditionStatement(codeBinaryOperatorExpression, codeAssignStatement));
            }
            return(xElementProperty);
        }
        private CodeMethodInvokeExpression SetNameMethodCall()
        {
            CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression("XTypedServices");

            CodeExpression[] codeExpressionArray = new CodeExpression[] { CodeDomHelper.This(), CodeDomHelper.CreateFieldReference("this", this.memberName) };
            return(new CodeMethodInvokeExpression(codeTypeReferenceExpression, "SetName", codeExpressionArray));
        }
        public static CodeTypeMember AddBrowseNever(CodeTypeMember member)
        {
            CodeAttributeArgument[]  codeAttributeArgument = new CodeAttributeArgument[] { new CodeAttributeArgument(CodeDomHelper.CreateFieldReference("DebuggerBrowsableState", "Never")) };
            CodeAttributeDeclaration browsableNever        = new CodeAttributeDeclaration("DebuggerBrowsable", codeAttributeArgument);

            if (member.CustomAttributes == null)
            {
                member.CustomAttributes = new CodeAttributeDeclarationCollection();
            }
            member.CustomAttributes.Add(browsableNever);
            return(member);
        }
Exemple #7
0
        public static CodeExpression CreateFacets(ClrSimpleTypeInfo type)
        {
            CompiledFacets facets = type.RestrictionFacets;

            CodeObjectCreateExpression createFacets = new CodeObjectCreateExpression();

            createFacets.CreateType = new CodeTypeReference(Constants.RestrictionFacets);

            RestrictionFlags flags = facets.Flags;

            if (flags == 0)
            {
                return(new CodePrimitiveExpression(null));
            }
            else
            {
                CodeCastExpression cast = new CodeCastExpression(new CodeTypeReference(Constants.RestrictionFlags),
                                                                 new CodePrimitiveExpression(
                                                                     System.Convert.ToInt32(flags, CultureInfo.InvariantCulture.NumberFormat)));
                createFacets.Parameters.Add(cast);
            }


            if ((flags & RestrictionFlags.Enumeration) != 0)
            {
                CodeArrayCreateExpression enums = new CodeArrayCreateExpression();
                enums.CreateType = new CodeTypeReference("System.Object");

                foreach (object o in facets.Enumeration)
                {
                    GetCreateValueExpression(o, type, enums.Initializers);
                }

                createFacets.Parameters.Add(enums);
            }
            else
            {
                createFacets.Parameters.Add(new CodePrimitiveExpression(null));
            }

            int fractionDigits = default(int);

            if ((flags & RestrictionFlags.FractionDigits) != 0)
            {
                fractionDigits = facets.FractionDigits;
            }

            createFacets.Parameters.Add(new CodePrimitiveExpression(fractionDigits));

            int length = default(int);

            if ((flags & RestrictionFlags.Length) != 0)
            {
                length = facets.Length;
            }

            createFacets.Parameters.Add(new CodePrimitiveExpression(length));

            object maxExclusive = default(object);

            if ((flags & RestrictionFlags.MaxExclusive) != 0)
            {
                maxExclusive = facets.MaxExclusive;
            }

            GetCreateValueExpression(maxExclusive, type, createFacets.Parameters);


            object maxInclusive = default(object);

            if ((flags & RestrictionFlags.MaxInclusive) != 0)
            {
                maxInclusive = facets.MaxInclusive;
            }

            GetCreateValueExpression(maxInclusive, type, createFacets.Parameters);


            int maxLength = default(int);

            if ((flags & RestrictionFlags.MaxLength) != 0)
            {
                maxLength = facets.MaxLength;
            }

            createFacets.Parameters.Add(new CodePrimitiveExpression(maxLength));

            object minExclusive = default(object);

            if ((flags & RestrictionFlags.MinExclusive) != 0)
            {
                minExclusive = facets.MinExclusive;
            }

            GetCreateValueExpression(minExclusive, type, createFacets.Parameters);


            object minInclusive = default(object);

            if ((flags & RestrictionFlags.MinInclusive) != 0)
            {
                minInclusive = facets.MinInclusive;
            }

            GetCreateValueExpression(minInclusive, type, createFacets.Parameters);


            int minLength = default(int);

            if ((flags & RestrictionFlags.MinLength) != 0)
            {
                minLength = facets.MinLength;
            }

            createFacets.Parameters.Add(new CodePrimitiveExpression(minLength));

            if ((flags & RestrictionFlags.Pattern) != 0)
            {
                CodeArrayCreateExpression patternStrs = new CodeArrayCreateExpression();
                patternStrs.CreateType = new CodeTypeReference(XTypedServices.typeOfString);

                foreach (object o in facets.Patterns)
                {
                    string str = o.ToString();
                    patternStrs.Initializers.Add(new CodePrimitiveExpression(str));
                }

                createFacets.Parameters.Add(patternStrs);
            }
            else
            {
                createFacets.Parameters.Add(new CodePrimitiveExpression(null));
            }

            int totalDigits = default(int);

            if ((flags & RestrictionFlags.TotalDigits) != 0)
            {
                totalDigits = facets.TotalDigits;
            }

            createFacets.Parameters.Add(new CodePrimitiveExpression(totalDigits));

            XmlSchemaWhiteSpace ws = facets.WhiteSpace;

            createFacets.Parameters.Add(
                CodeDomHelper.CreateFieldReference(Constants.XmlSchemaWhiteSpace, ws.ToString()));


            return(createFacets);
        }
Exemple #8
0
        internal void AddTypeToTypeManager(CodeStatementCollection dictionaryStatements, string dictionaryName)
        {
            string typeRef = string.Concat("global::", this.clrTypeInfo.clrFullTypeName);

            CodeExpression[] codeExpressionArray = new CodeExpression[] { CodeDomHelper.XNameGetExpression(this.clrTypeInfo.schemaName, this.clrTypeInfo.schemaNs), CodeDomHelper.Typeof(typeRef) };
            dictionaryStatements.Add(CodeDomHelper.CreateMethodCallFromField(dictionaryName, "Add", codeExpressionArray));
        }
Exemple #9
0
        private void CreateXRoot(CodeNamespace codeNamespace, string rootName, List <CodeTypeDeclaration> elements,
                                 List <CodeNamespace> namespaces, GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public)
        {
            LocalSymbolTable lst = new LocalSymbolTable();

            CodeTypeDeclaration xroot = CodeDomHelper.CreateTypeDeclaration(rootName, null, visibility);

            //Create Methods
            CodeMemberField docField = CodeDomHelper.CreateMemberField("doc",
                                                                       "XDocument",
                                                                       false, MemberAttributes.Private);

            CodeMemberField rootField = CodeDomHelper.CreateMemberField("rootObject",
                                                                        Constants.XTypedElement,
                                                                        false, MemberAttributes.Private);

            xroot.Members.Add(docField);
            xroot.Members.Add(rootField);


            lst.Init(rootName);
            lst.RegisterMember("doc");
            lst.RegisterMember("rootObject");
            lst.RegisterMember("Load");
            lst.RegisterMember("Parse");
            lst.RegisterMember("Save");
            lst.RegisterMember("XDocument");
            lst.RegisterMember("Root");

            // Constructor
            xroot.Members.Add(CodeDomHelper.CreateConstructor(MemberAttributes.Private));

            //Load Methods
            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load",
                                                              new string[][] { new string[] { "System.String", "xmlFile" } }, visibility));

            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][]
            {
                new string[] { "System.String", "xmlFile" },
                new string[] { "LoadOptions", "options" }
            }, visibility));


            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load",
                                                              new string[][] { new string[] { "TextReader", "textReader" } }, visibility));

            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][]
            {
                new string[] { "TextReader", "textReader" },
                new string[] { "LoadOptions", "options" }
            }, visibility));


            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load",
                                                              new string[][] { new string[] { "XmlReader", "xmlReader" } }, visibility));


            //Parse Methods
            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse",
                                                              new string[][] { new string[] { "System.String", "text" } }, visibility));

            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][]
            {
                new string[] { "System.String", "text" },
                new string[] { "LoadOptions", "options" }
            }, visibility));


            //Save Methods
            xroot.Members.Add(
                CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "System.String", "fileName" } }, visibility));
            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]
                                                            { new string[] { "TextWriter", "textWriter" } }, visibility));
            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][] { new string[] { "XmlWriter", "writer" } }, visibility));

            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]
            {
                new string[] { "TextWriter", "textWriter" },
                new string[] { "SaveOptions", "options" }
            }, visibility));
            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]
            {
                new string[] { "System.String", "fileName" },
                new string[] { "SaveOptions", "options" }
            }, visibility));

            CodeMemberProperty docProp = CodeDomHelper.CreateProperty("XDocument",
                                                                      "XDocument",
                                                                      docField,
                                                                      visibility.ToMemberAttribute(),
                                                                      false);

            xroot.Members.Add(docProp);

            CodeMemberProperty rootProp = CodeDomHelper.CreateProperty("Root",
                                                                       "XTypedElement",
                                                                       rootField,
                                                                       visibility.ToMemberAttribute(),
                                                                       false);

            xroot.Members.Add(rootProp);

            for (int i = 0; i < elements.Count; i++)
            {
                string typeName   = elements[i].Name;
                string fqTypeName = (namespaces == null || namespaces[i].Name == String.Empty)
                    ? typeName
                    : "global::" + namespaces[i].Name + "." + typeName;

                xroot.Members.Add(CodeDomHelper.CreateXRootFunctionalConstructor(fqTypeName, visibility));
                xroot.Members.Add(CodeDomHelper.CreateXRootGetter(typeName, fqTypeName, lst, visibility));
            }

            codeNamespace.Types.Add(xroot);
        }
Exemple #10
0
        internal void CreateServicesMembers()
        {
            CodeMethodInvokeExpression callClone;

            CodeExpression[] codeCastExpression;
            string           innerType     = this.InnerType;
            string           clrTypeName   = this.clrTypeInfo.clrtypeName;
            bool             useAutoTyping = (this.clrTypeInfo.IsAbstract ? true : this.clrTypeInfo.IsSubstitutionHead);

            if (this.clrTypeInfo.typeOrigin == SchemaOrigin.Element)
            {
                CodeTypeMember load       = CodeDomHelper.CreateStaticMethod("Load", clrTypeName, innerType, "xmlFile", "System.String", useAutoTyping);
                CodeTypeMember loadReader = CodeDomHelper.CreateStaticMethod("Load", clrTypeName, innerType, "xmlFile", "System.IO.TextReader", useAutoTyping);
                CodeTypeMember parse      = CodeDomHelper.CreateStaticMethod("Parse", clrTypeName, innerType, "xml", "System.String", useAutoTyping);
                if (!this.clrTypeInfo.IsDerived)
                {
                    this.decl.Members.Add(CodeDomHelper.CreateSave("xmlFile", "System.String"));
                    this.decl.Members.Add(CodeDomHelper.CreateSave("tw", "System.IO.TextWriter"));
                    this.decl.Members.Add(CodeDomHelper.CreateSave("xmlWriter", "System.Xml.XmlWriter"));
                }
                else
                {
                    CodeTypeMember attributes = load;
                    attributes.Attributes = attributes.Attributes | MemberAttributes.New;
                    CodeTypeMember codeTypeMember = parse;
                    codeTypeMember.Attributes = codeTypeMember.Attributes | MemberAttributes.New;
                }
                this.decl.Members.Add(load);
                this.decl.Members.Add(loadReader);
                this.decl.Members.Add(parse);
            }
            CodeTypeMember cast = CodeDomHelper.CreateCast(clrTypeName, innerType, useAutoTyping);

            this.decl.Members.Add(cast);
            if (!this.clrTypeInfo.IsAbstract)
            {
                CodeMemberMethod clone = CodeDomHelper.CreateMethod("Clone", MemberAttributes.Override | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public, new CodeTypeReference("XTypedElement"));
                if (innerType != null)
                {
                    callClone = CodeDomHelper.CreateMethodCall(new CodePropertyReferenceExpression(CodeDomHelper.This(), "Content"), "Clone", new CodeExpression[0]);
                    CodeStatementCollection statements = clone.Statements;
                    codeCastExpression = new CodeExpression[] { new CodeCastExpression(new CodeTypeReference(innerType), callClone) };
                    statements.Add(new CodeMethodReturnStatement(new CodeObjectCreateExpression(clrTypeName, codeCastExpression)));
                }
                else
                {
                    CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression("XTypedServices");
                    string str = string.Concat("CloneXTypedElement<", clrTypeName, ">");
                    codeCastExpression = new CodeExpression[] { new CodeThisReferenceExpression() };
                    callClone          = CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression, str, codeCastExpression);
                    clone.Statements.Add(new CodeMethodReturnStatement(callClone));
                }
                this.decl.Members.Add(clone);
            }
        }
Exemple #11
0
        internal static CodeTypeDeclaration CreateTypeManager(XmlQualifiedName rootElementName, bool enableServiceReference, CodeStatementCollection typeDictionaryStatements, CodeStatementCollection elementDictionaryStatements, CodeStatementCollection wrapperDictionaryStatements)
        {
            string servicesClassName             = NameGenerator.GetServicesClassName();
            CodeTypeDeclaration servicesTypeDecl = new CodeTypeDeclaration(servicesClassName)
            {
                Attributes = MemberAttributes.Public
            };
            CodeMemberField     singletonField            = CodeDomHelper.CreateMemberField("typeManagerSingleton", servicesClassName, MemberAttributes.Static, true);
            CodeMemberProperty  singletonProperty         = CodeDomHelper.CreateProperty("Instance", null, singletonField, MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public, false);
            MemberAttributes    privateStatic             = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.Assembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Private;
            CodeTypeConstructor staticServicesConstructor = new CodeTypeConstructor();
            CodeTypeReference   returnType        = CodeDomHelper.CreateDictionaryType("XName", "System.Type");
            CodeTypeReference   wrapperReturnType = CodeDomHelper.CreateDictionaryType("System.Type", "System.Type");
            CodeMemberProperty  typeDictProperty  = null;

            if (typeDictionaryStatements.Count <= 0)
            {
                typeDictProperty = CodeDomHelper.CreateInterfaceImplProperty("GlobalTypeDictionary", "ILinqToXsdTypeManager", returnType);
                typeDictProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("XTypedServices"), "EmptyDictionary")));
            }
            else
            {
                typeDictProperty = CodeDomHelper.CreateInterfaceImplProperty("GlobalTypeDictionary", "ILinqToXsdTypeManager", returnType, "typeDictionary");
                CodeMemberField  staticTypeDictionary = CodeDomHelper.CreateDictionaryField("typeDictionary", "XName", "System.Type");
                CodeMemberMethod buildTypeDictionary  = CodeDomHelper.CreateMethod("BuildTypeDictionary", privateStatic, null);
                buildTypeDictionary.Statements.AddRange(typeDictionaryStatements);
                staticServicesConstructor.Statements.Add(CodeDomHelper.CreateMethodCall(null, "BuildTypeDictionary", new CodeExpression[0]));
                servicesTypeDecl.Members.Add(staticTypeDictionary);
                servicesTypeDecl.Members.Add(buildTypeDictionary);
            }
            CodeMemberProperty elementDictProperty = null;

            if (elementDictionaryStatements.Count <= 0)
            {
                elementDictProperty = CodeDomHelper.CreateInterfaceImplProperty("GlobalElementDictionary", "ILinqToXsdTypeManager", returnType);
                elementDictProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("XTypedServices"), "EmptyDictionary")));
            }
            else
            {
                elementDictProperty = CodeDomHelper.CreateInterfaceImplProperty("GlobalElementDictionary", "ILinqToXsdTypeManager", returnType, "elementDictionary");
                CodeMemberField  staticElementDictionary = CodeDomHelper.CreateDictionaryField("elementDictionary", "XName", "System.Type");
                CodeMemberMethod buildElementDictionary  = CodeDomHelper.CreateMethod("BuildElementDictionary", privateStatic, null);
                buildElementDictionary.Statements.AddRange(elementDictionaryStatements);
                staticServicesConstructor.Statements.Add(CodeDomHelper.CreateMethodCall(null, "BuildElementDictionary", new CodeExpression[0]));
                servicesTypeDecl.Members.Add(staticElementDictionary);
                servicesTypeDecl.Members.Add(buildElementDictionary);
            }
            CodeMemberProperty wrapperDictProperty = null;

            if (wrapperDictionaryStatements.Count <= 0)
            {
                wrapperDictProperty = CodeDomHelper.CreateInterfaceImplProperty("RootContentTypeMapping", "ILinqToXsdTypeManager", wrapperReturnType);
                wrapperDictProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("XTypedServices"), "EmptyTypeMappingDictionary")));
            }
            else
            {
                wrapperDictProperty = CodeDomHelper.CreateInterfaceImplProperty("RootContentTypeMapping", "ILinqToXsdTypeManager", wrapperReturnType, "wrapperDictionary");
                CodeMemberField  staticWrapperDictionary = CodeDomHelper.CreateDictionaryField("wrapperDictionary", "System.Type", "System.Type");
                CodeMemberMethod buildWrapperDictionary  = CodeDomHelper.CreateMethod("BuildWrapperDictionary", privateStatic, null);
                buildWrapperDictionary.Statements.AddRange(wrapperDictionaryStatements);
                staticServicesConstructor.Statements.Add(CodeDomHelper.CreateMethodCall(null, "BuildWrapperDictionary", new CodeExpression[0]));
                servicesTypeDecl.Members.Add(staticWrapperDictionary);
                servicesTypeDecl.Members.Add(buildWrapperDictionary);
            }
            string            schemaSetFieldName = "schemaSet";
            CodeTypeReference schemaSetType      = new CodeTypeReference("XmlSchemaSet");
            CodeMemberField   schemaSetField     = new CodeMemberField(schemaSetType, schemaSetFieldName)
            {
                Attributes = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.Assembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Private
            };
            CodeMemberMethod addSchemasMethod = CodeDomHelper.CreateMethod("AddSchemas", MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyOrAssembly, null);

            addSchemasMethod.Parameters.Add(new CodeParameterDeclarationExpression("XmlSchemaSet", "schemas"));
            CodeStatementCollection         statements = addSchemasMethod.Statements;
            CodeVariableReferenceExpression codeVariableReferenceExpression = new CodeVariableReferenceExpression("schemas");

            CodeExpression[] codeFieldReferenceExpression = new CodeExpression[] { new CodeFieldReferenceExpression(null, schemaSetFieldName) };
            statements.Add(CodeDomHelper.CreateMethodCall(codeVariableReferenceExpression, "Add", codeFieldReferenceExpression));
            CodeTypeReferenceExpression  interLockedType              = new CodeTypeReferenceExpression("System.Threading.Interlocked");
            CodeMemberProperty           schemaSetProperty            = CodeDomHelper.CreateInterfaceImplProperty("Schemas", "ILinqToXsdTypeManager", schemaSetType);
            CodeFieldReferenceExpression schemaSetFieldRef            = new CodeFieldReferenceExpression(null, schemaSetFieldName);
            CodeDirectionExpression      schemaSetParam               = new CodeDirectionExpression(FieldDirection.Ref, schemaSetFieldRef);
            CodeStatementCollection      getStatements                = schemaSetProperty.GetStatements;
            CodeBinaryOperatorExpression codeBinaryOperatorExpression = new CodeBinaryOperatorExpression(schemaSetFieldRef, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));

            CodeStatement[] codeVariableDeclarationStatement = new CodeStatement[] { new CodeVariableDeclarationStatement(schemaSetType, "tempSet", new CodeObjectCreateExpression(schemaSetType, new CodeExpression[0])), null };
            codeFieldReferenceExpression        = new CodeExpression[] { schemaSetParam, new CodeVariableReferenceExpression("tempSet"), new CodePrimitiveExpression(null) };
            codeVariableDeclarationStatement[1] = new CodeExpressionStatement(CodeDomHelper.CreateMethodCall(interLockedType, "CompareExchange", codeFieldReferenceExpression));
            getStatements.Add(new CodeConditionStatement(codeBinaryOperatorExpression, codeVariableDeclarationStatement));
            schemaSetProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(schemaSetFieldName)));
            schemaSetProperty.SetStatements.Add(new CodeAssignStatement(schemaSetFieldRef, new CodePropertySetValueReferenceExpression()));
            servicesTypeDecl.Members.Add(schemaSetField);
            servicesTypeDecl.Members.Add(schemaSetProperty);
            servicesTypeDecl.Members.Add(addSchemasMethod);
            servicesTypeDecl.Members.Add(typeDictProperty);
            servicesTypeDecl.Members.Add(elementDictProperty);
            servicesTypeDecl.Members.Add(wrapperDictProperty);
            servicesTypeDecl.BaseTypes.Add("ILinqToXsdTypeManager");
            CodeMemberMethod getRootType = new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public,
                Name       = "GetRootType",
                ReturnType = new CodeTypeReference("System.Type")
            };

            if (!rootElementName.IsEmpty)
            {
                CodeStatementCollection      codeStatementCollection       = getRootType.Statements;
                CodeFieldReferenceExpression codeFieldReferenceExpression1 = CodeDomHelper.CreateFieldReference(null, "elementDictionary");
                codeFieldReferenceExpression = new CodeExpression[] { CodeDomHelper.XNameGetExpression(rootElementName.Name, rootElementName.Namespace) };
                codeStatementCollection.Add(new CodeMethodReturnStatement(new CodeIndexerExpression(codeFieldReferenceExpression1, codeFieldReferenceExpression)));
            }
            else
            {
                getRootType.Statements.Add(new CodeMethodReturnStatement(CodeDomHelper.Typeof("Xml.Schema.Linq.XTypedElement")));
            }
            servicesTypeDecl.Members.Add(staticServicesConstructor);
            servicesTypeDecl.Members.Add(getRootType);
            servicesTypeDecl.Members.Add(singletonField);
            servicesTypeDecl.Members.Add(singletonProperty);
            return(servicesTypeDecl);
        }
Exemple #12
0
 internal virtual void CreateDefaultConstructor(List <ClrAnnotation> annotations)
 {
     this.decl.Members.Add(TypeBuilder.ApplyAnnotations(CodeDomHelper.CreateConstructor(MemberAttributes.Public), annotations, null));
 }
        public static CodeExpression CreateFacets(ClrSimpleTypeInfo type)
        {
            //object o = null;
            CodeExpression             codePrimitiveExpression;
            CompiledFacets             facets       = type.RestrictionFacets;
            CodeObjectCreateExpression createFacets = new CodeObjectCreateExpression()
            {
                CreateType = new CodeTypeReference("Xml.Schema.Linq.RestrictionFacets")
            };

            Xml.Schema.Linq.RestrictionFlags flags = facets.Flags;
            if ((int)flags != 0)
            {
                CodeCastExpression cast = new CodeCastExpression(new CodeTypeReference("Xml.Schema.Linq.RestrictionFlags"), new CodePrimitiveExpression((object)Convert.ToInt32(flags, CultureInfo.InvariantCulture.NumberFormat)));
                createFacets.Parameters.Add(cast);
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Enumeration) == 0)
                {
                    createFacets.Parameters.Add(new CodePrimitiveExpression(null));
                }
                else
                {
                    CodeArrayCreateExpression enums = new CodeArrayCreateExpression()
                    {
                        CreateType = new CodeTypeReference("System.Object")
                    };
                    foreach (object o in facets.Enumeration)
                    {
                        SimpleTypeCodeDomHelper.GetCreateValueExpression(o, type, enums.Initializers);
                    }
                    createFacets.Parameters.Add(enums);
                }
                int fractionDigits = 0;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.FractionDigits) != 0)
                {
                    fractionDigits = facets.FractionDigits;
                }
                createFacets.Parameters.Add(new CodePrimitiveExpression((object)fractionDigits));
                int length = 0;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Length) != 0)
                {
                    length = facets.Length;
                }
                createFacets.Parameters.Add(new CodePrimitiveExpression((object)length));
                object maxExclusive = null;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxExclusive) != 0)
                {
                    maxExclusive = facets.MaxExclusive;
                }
                SimpleTypeCodeDomHelper.GetCreateValueExpression(maxExclusive, type, createFacets.Parameters);
                object maxInclusive = null;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxInclusive) != 0)
                {
                    maxInclusive = facets.MaxInclusive;
                }
                SimpleTypeCodeDomHelper.GetCreateValueExpression(maxInclusive, type, createFacets.Parameters);
                int maxLength = 0;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxLength) != 0)
                {
                    maxLength = facets.MaxLength;
                }
                createFacets.Parameters.Add(new CodePrimitiveExpression((object)maxLength));
                object minExclusive = null;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinExclusive) != 0)
                {
                    minExclusive = facets.MinExclusive;
                }
                SimpleTypeCodeDomHelper.GetCreateValueExpression(minExclusive, type, createFacets.Parameters);
                object minInclusive = null;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinInclusive) != 0)
                {
                    minInclusive = facets.MinInclusive;
                }
                SimpleTypeCodeDomHelper.GetCreateValueExpression(minInclusive, type, createFacets.Parameters);
                int minLength = 0;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinLength) != 0)
                {
                    minLength = facets.MinLength;
                }
                createFacets.Parameters.Add(new CodePrimitiveExpression((object)minLength));
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Pattern) == 0)
                {
                    createFacets.Parameters.Add(new CodePrimitiveExpression(null));
                }
                else
                {
                    CodeArrayCreateExpression patternStrs = new CodeArrayCreateExpression()
                    {
                        CreateType = new CodeTypeReference(XTypedServices.typeOfString)
                    };
                    foreach (object pattern in facets.Patterns)
                    {
                        string str = pattern.ToString();
                        patternStrs.Initializers.Add(new CodePrimitiveExpression(str));
                    }
                    createFacets.Parameters.Add(patternStrs);
                }
                int totalDigits = 0;
                if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.TotalDigits) != 0)
                {
                    totalDigits = facets.TotalDigits;
                }
                createFacets.Parameters.Add(new CodePrimitiveExpression((object)totalDigits));
                Xml.Schema.Linq.XmlSchemaWhiteSpace ws = facets.WhiteSpace;
                createFacets.Parameters.Add(CodeDomHelper.CreateFieldReference("XmlSchemaWhiteSpace", ws.ToString()));
                codePrimitiveExpression = createFacets;
            }
            else
            {
                codePrimitiveExpression = new CodePrimitiveExpression(null);
            }
            return(codePrimitiveExpression);
        }
        private static CodeExpression CreateTypedValueExpression(XmlSchemaDatatype dataType, object value)
        {
            CodeExpression codePrimitiveExpression;

            CodeExpression[] codeExpressionArray;
            switch (dataType.TypeCode)
            {
            case XmlTypeCode.None:
            case XmlTypeCode.Item:
            case XmlTypeCode.AnyAtomicType:
            case XmlTypeCode.Idref:
            case XmlTypeCode.Entity:
            {
                throw new InvalidOperationException();
            }

            case XmlTypeCode.Node:
            case XmlTypeCode.Document:
            case XmlTypeCode.Element:
            case XmlTypeCode.Attribute:
            case XmlTypeCode.Namespace:
            case XmlTypeCode.ProcessingInstruction:
            case XmlTypeCode.Comment:
            case XmlTypeCode.Text:
            case XmlTypeCode.UntypedAtomic:
            {
                throw new InvalidOperationException();
            }

            case XmlTypeCode.String:
            case XmlTypeCode.Notation:
            case XmlTypeCode.NormalizedString:
            case XmlTypeCode.Token:
            case XmlTypeCode.Language:
            case XmlTypeCode.Id:
            {
                string str = value as string;
                Debug.Assert(str != null);
                codePrimitiveExpression = new CodePrimitiveExpression(str);
                break;
            }

            case XmlTypeCode.Boolean:
            {
                Debug.Assert(value is bool);
                codePrimitiveExpression = new CodePrimitiveExpression(value);
                break;
            }

            case XmlTypeCode.Decimal:
            case XmlTypeCode.Integer:
            case XmlTypeCode.NonPositiveInteger:
            case XmlTypeCode.NegativeInteger:
            case XmlTypeCode.Long:
            case XmlTypeCode.Int:
            case XmlTypeCode.Short:
            case XmlTypeCode.Byte:
            case XmlTypeCode.NonNegativeInteger:
            case XmlTypeCode.UnsignedLong:
            case XmlTypeCode.UnsignedInt:
            case XmlTypeCode.UnsignedShort:
            case XmlTypeCode.UnsignedByte:
            case XmlTypeCode.PositiveInteger:
            {
                codePrimitiveExpression = new CodePrimitiveExpression(value);
                break;
            }

            case XmlTypeCode.Float:
            case XmlTypeCode.Double:
            {
                Debug.Assert((value is double?true: value is float));
                codePrimitiveExpression = new CodePrimitiveExpression(value);
                break;
            }

            case XmlTypeCode.Duration:
            {
                Debug.Assert(value is TimeSpan);
                TimeSpan ts   = (TimeSpan)value;
                Type     type = typeof(TimeSpan);
                codeExpressionArray     = new CodeExpression[] { new CodePrimitiveExpression((object)ts.Ticks) };
                codePrimitiveExpression = new CodeObjectCreateExpression(type, codeExpressionArray);
                break;
            }

            case XmlTypeCode.DateTime:
            case XmlTypeCode.Time:
            case XmlTypeCode.Date:
            case XmlTypeCode.GYearMonth:
            case XmlTypeCode.GYear:
            case XmlTypeCode.GMonthDay:
            case XmlTypeCode.GDay:
            case XmlTypeCode.GMonth:
            {
                Debug.Assert(value is DateTime);
                DateTime dt    = (DateTime)value;
                Type     type1 = typeof(DateTime);
                codeExpressionArray     = new CodeExpression[] { new CodePrimitiveExpression((object)dt.Ticks) };
                codePrimitiveExpression = new CodeObjectCreateExpression(type1, codeExpressionArray);
                break;
            }

            case XmlTypeCode.HexBinary:
            case XmlTypeCode.Base64Binary:
            {
                codePrimitiveExpression = SimpleTypeCodeDomHelper.CreateByteArrayExpression(value);
                break;
            }

            case XmlTypeCode.AnyUri:
            {
                Debug.Assert(value is Uri);
                Type type2 = typeof(Uri);
                codeExpressionArray     = new CodeExpression[] { new CodePrimitiveExpression(((Uri)value).OriginalString) };
                codePrimitiveExpression = new CodeObjectCreateExpression(type2, codeExpressionArray);
                break;
            }

            case XmlTypeCode.QName:
            {
                XmlQualifiedName qname = value as XmlQualifiedName;
                Type             type3 = typeof(XmlQualifiedName);
                codeExpressionArray     = new CodeExpression[] { new CodePrimitiveExpression(qname.Name), new CodePrimitiveExpression(qname.Namespace) };
                codePrimitiveExpression = new CodeObjectCreateExpression(type3, codeExpressionArray);
                break;
            }

            case XmlTypeCode.NmToken:
            case XmlTypeCode.Name:
            case XmlTypeCode.NCName:
            {
                CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression(typeof(XmlConvert));
                codeExpressionArray     = new CodeExpression[] { new CodePrimitiveExpression(value.ToString()) };
                codePrimitiveExpression = CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression, "EncodeName", codeExpressionArray);
                break;
            }

            default:
            {
                throw new InvalidOperationException();
            }
            }
            return(codePrimitiveExpression);
        }
        public static CodeMemberMethod CreateXRootMethod(string returnType, string methodName, string[][] paramList)
        {
            CodeTypeReference xRootType    = new CodeTypeReference(returnType);
            CodeMemberMethod  staticMethod = new CodeMemberMethod()
            {
                Name       = methodName,
                Attributes = MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.FamilyAndAssembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Public,
                ReturnType = xRootType
            };

            CodeExpression[] parameterExp = new CodeExpression[(int)paramList.Length];
            for (int i = 0; i < (int)paramList.Length; i++)
            {
                string[] paramRef = paramList[i];
                staticMethod.Parameters.Add(CodeDomHelper.CreateParameter(paramRef[1], paramRef[0]));
                parameterExp[i] = new CodeVariableReferenceExpression(paramRef[1]);
            }
            CodeExpression rootExp = new CodeVariableReferenceExpression("root");
            CodeExpression doc     = new CodeFieldReferenceExpression(rootExp, "doc");

            staticMethod.Statements.Add(new CodeVariableDeclarationStatement(xRootType, "root", new CodeObjectCreateExpression(xRootType, new CodeExpression[0])));
            staticMethod.Statements.Add(new CodeAssignStatement(doc, CodeDomHelper.CreateMethodCall(new CodeTypeReferenceExpression("XDocument"), methodName, parameterExp)));
            CodeStatementCollection     statements = staticMethod.Statements;
            CodeTypeReferenceExpression codeTypeReferenceExpression = new CodeTypeReferenceExpression("XTypedServices");

            CodeExpression[] codePropertyReferenceExpression = new CodeExpression[] { new CodePropertyReferenceExpression(doc, "Root"), CodeDomHelper.SingletonTypeManager() };
            statements.Add(new CodeVariableDeclarationStatement("XTypedElement", "typedRoot", CodeDomHelper.CreateMethodCall(codeTypeReferenceExpression, "ToXTypedElement", codePropertyReferenceExpression)));
            CodeStatementCollection      codeStatementCollection      = staticMethod.Statements;
            CodeBinaryOperatorExpression codeBinaryOperatorExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("typedRoot"), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));

            CodeStatement[] codeThrowExceptionStatement = new CodeStatement[1];
            codePropertyReferenceExpression = new CodeExpression[] { new CodePrimitiveExpression("Invalid root element in xml document.") };
            codeThrowExceptionStatement[0]  = new CodeThrowExceptionStatement(new CodeObjectCreateExpression("LinqToXsdException", codePropertyReferenceExpression));
            codeStatementCollection.Add(new CodeConditionStatement(codeBinaryOperatorExpression, codeThrowExceptionStatement));
            staticMethod.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(rootExp, "rootObject"), new CodeVariableReferenceExpression("typedRoot")));
            staticMethod.Statements.Add(new CodeMethodReturnStatement(rootExp));
            return(staticMethod);
        }
Exemple #16
0
        private CodeMemberProperty BuildLocalElementDictionary()
        {
            CodeMemberProperty localDictionaryProperty = CodeDomHelper.CreateInterfaceImplProperty("LocalElementsDictionary", "IXMetaData", CodeDomHelper.CreateDictionaryType("XName", "System.Type"));
            CodeMemberField    localDictionaryField    = CodeDomHelper.CreateDictionaryField("localElementDictionary", "XName", "System.Type");
            CodeMemberMethod   localDictionaryMethod   = CodeDomHelper.CreateMethod("BuildElementDictionary", MemberAttributes.Abstract | MemberAttributes.Final | MemberAttributes.Static | MemberAttributes.Assembly | MemberAttributes.FamilyOrAssembly | MemberAttributes.Private, null);

            localDictionaryMethod.Statements.AddRange(this.propertyDictionaryAddStatements);
            this.decl.Members.Add(localDictionaryField);
            this.decl.Members.Add(localDictionaryMethod);
            localDictionaryProperty.GetStatements.Add(new CodeMethodReturnStatement(CodeDomHelper.CreateFieldReference(null, "localElementDictionary")));
            CodeDomHelper.AddBrowseNever(localDictionaryProperty);
            CodeDomHelper.AddBrowseNever(localDictionaryField);
            return(localDictionaryProperty);
        }
        public static CodeMethodInvokeExpression XNameGetExpression(string name, string ns)
        {
            CodeMethodInvokeExpression codeMethodInvokeExpression = CodeDomHelper.XNameGetExpression(new CodePrimitiveExpression(name), new CodePrimitiveExpression(ns));

            return(codeMethodInvokeExpression);
        }
Exemple #18
0
 internal override void CreateProperty(ClrBasePropertyInfo propertyInfo, List <ClrAnnotation> annotations)
 {
     if ((!this.clrTypeInfo.InlineBaseType ? false : propertyInfo.FromBaseType))
     {
         propertyInfo.IsNew = true;
     }
     this.propertyBuilder.GenerateCode(propertyInfo, annotations);
     if ((propertyInfo.ContentType != ContentType.Property ? false : !propertyInfo.IsDuplicate))
     {
         CodeStatementCollection codeStatementCollection = this.propertyDictionaryAddStatements;
         CodeExpression[]        codeExpressionArray     = new CodeExpression[] { CodeDomHelper.XNameGetExpression(propertyInfo.SchemaName, propertyInfo.PropertyNs), CodeDomHelper.Typeof(propertyInfo.ClrTypeName) };
         codeStatementCollection.Add(CodeDomHelper.CreateMethodCallFromField("localElementDictionary", "Add", codeExpressionArray));
     }
 }
Exemple #19
0
        private static CodeExpression CreateTypedValueExpression(XmlSchemaDatatype dataType, object value)
        {
            XmlTypeCode typeCode = dataType.TypeCode;

            switch (typeCode)
            {
            case XmlTypeCode.String:
            case XmlTypeCode.Notation:
            case XmlTypeCode.NormalizedString:
            case XmlTypeCode.Token:
            case XmlTypeCode.Language:
            case XmlTypeCode.Id:
                string str = value as string;
                Debug.Assert(str != null);
                return(new CodePrimitiveExpression(str));

            case XmlTypeCode.AnyUri:
                Debug.Assert(value is Uri);
                return(new CodeObjectCreateExpression(typeof(Uri),
                                                      new CodePrimitiveExpression(((Uri)value).OriginalString)));

            case XmlTypeCode.QName:
                XmlQualifiedName qname = value as XmlQualifiedName;
                return(new CodeObjectCreateExpression(typeof(XmlQualifiedName),
                                                      new CodePrimitiveExpression(qname.Name), new CodePrimitiveExpression(qname.Namespace)));

            case XmlTypeCode.NmToken:
            case XmlTypeCode.Name:
            case XmlTypeCode.NCName:
                return(CodeDomHelper.CreateMethodCall(
                           new CodeTypeReferenceExpression(typeof(XmlConvert)),
                           "EncodeName",
                           new CodePrimitiveExpression(value.ToString())));

            case XmlTypeCode.Boolean:
                Debug.Assert(value is bool);
                return(new CodePrimitiveExpression(value));

            case XmlTypeCode.Float:
            case XmlTypeCode.Double:
                Debug.Assert(value is double || value is float);
                return(new CodePrimitiveExpression(value));

            case XmlTypeCode.Duration:
                Debug.Assert(value is TimeSpan);
                TimeSpan ts = (TimeSpan)value;
                return(new CodeObjectCreateExpression(typeof(TimeSpan), new CodePrimitiveExpression(ts.Ticks)));

            case XmlTypeCode.Time:
            case XmlTypeCode.Date:
            case XmlTypeCode.GYearMonth:
            case XmlTypeCode.GYear:
            case XmlTypeCode.GMonthDay:
            case XmlTypeCode.GDay:
            case XmlTypeCode.GMonth:
            case XmlTypeCode.DateTime:
                Debug.Assert(value is DateTime);
                DateTime dt = (DateTime)value;
                return(new CodeObjectCreateExpression(typeof(DateTime), new CodePrimitiveExpression(dt.Ticks)));

            case XmlTypeCode.Integer:
            case XmlTypeCode.NonPositiveInteger:
            case XmlTypeCode.NegativeInteger:
            case XmlTypeCode.Long:
            case XmlTypeCode.Int:
            case XmlTypeCode.Short:
            case XmlTypeCode.NonNegativeInteger:
            case XmlTypeCode.UnsignedLong:
            case XmlTypeCode.UnsignedInt:
            case XmlTypeCode.UnsignedShort:
            case XmlTypeCode.PositiveInteger:
            case XmlTypeCode.Decimal:
            case XmlTypeCode.Byte:
            case XmlTypeCode.UnsignedByte:
                return(new CodePrimitiveExpression(value));

            case XmlTypeCode.HexBinary:
            case XmlTypeCode.Base64Binary:
                return(CreateByteArrayExpression(value));

            case XmlTypeCode.None:
            case XmlTypeCode.Item:
            case XmlTypeCode.AnyAtomicType:
            case XmlTypeCode.Idref:
            case XmlTypeCode.Entity:
                throw new InvalidOperationException();

            default:
                throw new InvalidOperationException();
            }
        }
 private void CreateXRoot(CodeNamespace codeNamespace, string rootName, List <CodeTypeDeclaration> elements, List <CodeNamespace> namespaces)
 {
     if (codeNamespace != null)
     {
         LocalSymbolTable    lst       = new LocalSymbolTable(this.settings);
         CodeTypeDeclaration xroot     = CodeDomHelper.CreateTypeDeclaration(rootName, null);
         CodeMemberField     docField  = CodeDomHelper.CreateMemberField("doc", "XDocument", MemberAttributes.Private, false);
         CodeMemberField     rootField = CodeDomHelper.CreateMemberField("rootObject", "XTypedElement", MemberAttributes.Private, false);
         xroot.Members.Add(docField);
         xroot.Members.Add(rootField);
         lst.Init(rootName);
         lst.RegisterMember("doc");
         lst.RegisterMember("rootObject");
         lst.RegisterMember("Load");
         lst.RegisterMember("Parse");
         lst.RegisterMember("Save");
         lst.RegisterMember("XDocument");
         xroot.Members.Add(CodeDomHelper.CreateConstructor(MemberAttributes.Private));
         CodeTypeMemberCollection members = xroot.Members;
         string[][] strArrays             = new string[1][];
         string[]   strArrays1            = new string[] { "System.String", "xmlFile" };
         strArrays[0] = strArrays1;
         members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", strArrays));
         CodeTypeMemberCollection codeTypeMemberCollection = xroot.Members;
         strArrays    = new string[2][];
         strArrays1   = new string[] { "System.String", "xmlFile" };
         strArrays[0] = strArrays1;
         strArrays1   = new string[] { "LoadOptions", "options" };
         strArrays[1] = strArrays1;
         codeTypeMemberCollection.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", strArrays));
         CodeTypeMemberCollection members1 = xroot.Members;
         strArrays    = new string[1][];
         strArrays1   = new string[] { "TextReader", "textReader" };
         strArrays[0] = strArrays1;
         members1.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", strArrays));
         CodeTypeMemberCollection codeTypeMemberCollection1 = xroot.Members;
         strArrays    = new string[2][];
         strArrays1   = new string[] { "TextReader", "textReader" };
         strArrays[0] = strArrays1;
         strArrays1   = new string[] { "LoadOptions", "options" };
         strArrays[1] = strArrays1;
         codeTypeMemberCollection1.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", strArrays));
         CodeTypeMemberCollection members2 = xroot.Members;
         strArrays    = new string[1][];
         strArrays1   = new string[] { "XmlReader", "xmlReader" };
         strArrays[0] = strArrays1;
         members2.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", strArrays));
         CodeTypeMemberCollection codeTypeMemberCollection2 = xroot.Members;
         strArrays    = new string[1][];
         strArrays1   = new string[] { "System.String", "text" };
         strArrays[0] = strArrays1;
         codeTypeMemberCollection2.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", strArrays));
         CodeTypeMemberCollection members3 = xroot.Members;
         strArrays    = new string[2][];
         strArrays1   = new string[] { "System.String", "text" };
         strArrays[0] = strArrays1;
         strArrays1   = new string[] { "LoadOptions", "options" };
         strArrays[1] = strArrays1;
         members3.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", strArrays));
         CodeTypeMemberCollection codeTypeMemberCollection3 = xroot.Members;
         strArrays    = new string[1][];
         strArrays1   = new string[] { "System.String", "fileName" };
         strArrays[0] = strArrays1;
         codeTypeMemberCollection3.Add(CodeDomHelper.CreateXRootSave(strArrays));
         CodeTypeMemberCollection members4 = xroot.Members;
         strArrays    = new string[1][];
         strArrays1   = new string[] { "TextWriter", "textWriter" };
         strArrays[0] = strArrays1;
         members4.Add(CodeDomHelper.CreateXRootSave(strArrays));
         CodeTypeMemberCollection codeTypeMemberCollection4 = xroot.Members;
         strArrays    = new string[1][];
         strArrays1   = new string[] { "XmlWriter", "writer" };
         strArrays[0] = strArrays1;
         codeTypeMemberCollection4.Add(CodeDomHelper.CreateXRootSave(strArrays));
         CodeTypeMemberCollection members5 = xroot.Members;
         strArrays    = new string[2][];
         strArrays1   = new string[] { "TextWriter", "textWriter" };
         strArrays[0] = strArrays1;
         strArrays1   = new string[] { "SaveOptions", "options" };
         strArrays[1] = strArrays1;
         members5.Add(CodeDomHelper.CreateXRootSave(strArrays));
         CodeTypeMemberCollection codeTypeMemberCollection5 = xroot.Members;
         strArrays    = new string[2][];
         strArrays1   = new string[] { "System.String", "fileName" };
         strArrays[0] = strArrays1;
         strArrays1   = new string[] { "SaveOptions", "options" };
         strArrays[1] = strArrays1;
         codeTypeMemberCollection5.Add(CodeDomHelper.CreateXRootSave(strArrays));
         CodeMemberProperty prop = CodeDomHelper.CreateProperty("XDocument", "XDocument", docField, MemberAttributes.Public, false);
         xroot.Members.Add(prop);
         for (int i = 0; i < elements.Count; i++)
         {
             string typeName   = elements[i].Name;
             string fqTypeName = (namespaces == null || namespaces[i].Name == string.Empty ? typeName : string.Concat("global::", namespaces[i].Name, ".", typeName));
             xroot.Members.Add(CodeDomHelper.CreateXRootFunctionalConstructor(fqTypeName));
             xroot.Members.Add(CodeDomHelper.CreateXRootGetter(typeName, fqTypeName, lst));
         }
         codeNamespace.Types.Add(xroot);
     }
 }
Exemple #21
0
        public static CodeMemberMethod CreateXRootMethod(string returnType, string methodName, string[][] paramList,
                                                         GeneratedTypesVisibility visibility = GeneratedTypesVisibility.Public)
        {
            CodeTypeReference xRootType = new CodeTypeReference(returnType);

            CodeMemberMethod staticMethod = new CodeMemberMethod();

            staticMethod.Name       = methodName;
            staticMethod.Attributes = visibility.ToMemberAttribute() | MemberAttributes.Static;
            staticMethod.ReturnType = xRootType;
            CodeExpression[] parameterExp = new CodeExpression[paramList.Length];

            for (int i = 0; i < paramList.Length; i++)
            {
                string[] paramRef = paramList[i];
                // index 0 is the type name and index 1 is the parameter name
                staticMethod.Parameters.Add(CreateParameter(paramRef[1], paramRef[0]));
                parameterExp[i] = new CodeVariableReferenceExpression(paramRef[1]);
            }

            CodeExpression rootExp = new CodeVariableReferenceExpression("root");
            CodeExpression doc     = new CodeFieldReferenceExpression(rootExp, "doc");

            staticMethod.Statements.Add( //XRoot root = new XRoot;
                new CodeVariableDeclarationStatement(xRootType, "root",
                                                     new CodeObjectCreateExpression(xRootType)));

            staticMethod.Statements.Add( //root.doc = XDocument.Load(xmlFile);
                new CodeAssignStatement(
                    doc,
                    CreateMethodCall(new CodeTypeReferenceExpression("XDocument"), methodName,
                                     parameterExp)));

            staticMethod.Statements.Add( //XTypedElement typedRoot = XTypedServices.ToXTypedElement(....)
                new CodeVariableDeclarationStatement(
                    Constants.XTypedElement,
                    "typedRoot",
                    CreateMethodCall(
                        new CodeTypeReferenceExpression(Constants.XTypedServices),
                        Constants.ToXTypedElement,
                        new CodePropertyReferenceExpression(doc, "Root"),
                        CodeDomHelper.SingletonTypeManager())));

            staticMethod.Statements.Add( //if(typedRoot == null)
                new CodeConditionStatement(
                    new CodeBinaryOperatorExpression(
                        new CodeVariableReferenceExpression("typedRoot"),
                        CodeBinaryOperatorType.IdentityEquality,
                        new CodePrimitiveExpression(null)
                        ),
                    new CodeThrowExceptionStatement(
                        new CodeObjectCreateExpression(Constants.LinqToXsdException,
                                                       new CodePrimitiveExpression("Invalid root element in xml document."))
                        )));

            staticMethod.Statements.Add( //root.rootObject = typedRoot
                new CodeAssignStatement(
                    new CodeFieldReferenceExpression(rootExp, "rootObject"),
                    new CodeVariableReferenceExpression("typedRoot")));

            staticMethod.Statements.Add( //return root;
                new CodeMethodReturnStatement(rootExp));

            return(staticMethod);
        }
        public static CodeSnippetTypeMember CreateCast(string typeT, string typeT1, bool useAutoTyping)
        {
            string[] strArrays;
            CodeSnippetTypeMember castMember = new CodeSnippetTypeMember();

            if (!useAutoTyping)
            {
                strArrays       = new string[] { "         public static explicit operator ", typeT, "(XElement xe) { return ", "XTypedServices", ".ToXTypedElement<", CodeDomHelper.GetInnerType(typeT, typeT1), ">(xe,", NameGenerator.GetServicesClassName(), ".Instance as ILinqToXsdTypeManager); }" };
                castMember.Text = string.Concat(strArrays);
            }
            else
            {
                strArrays       = new string[] { "         public static explicit operator ", typeT, "(XElement xe) {  ", "return (", typeT, ")", "XTypedServices", ".ToXTypedElement(xe,", NameGenerator.GetServicesClassName(), ".Instance as ILinqToXsdTypeManager); }" };
                castMember.Text = string.Concat(strArrays);
            }
            return(castMember);
        }
        protected override void ImplementCommonIXMetaData()
        {
            CodeMemberProperty localElementDictionary = CodeDomHelper.CreateInterfaceImplProperty("LocalElementsDictionary", "IXMetaData", CodeDomHelper.CreateDictionaryType("XName", "System.Type"));

            localElementDictionary.GetStatements.Add(CodeDomHelper.CreateCastToInterface("IXMetaData", "schemaMetaData", "Content"));
            localElementDictionary.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("schemaMetaData"), "LocalElementsDictionary")));
            CodeMemberProperty contentProperty = CodeDomHelper.CreateInterfaceImplProperty("Content", "IXMetaData", new CodeTypeReference("XTypedElement"));

            contentProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Content")));
            this.decl.Members.Add(localElementDictionary);
            this.decl.Members.Add(contentProperty);
        }
        public static CodeMethodInvokeExpression CreateMethodCallFromField(string fieldName, string methodName, params CodeExpression[] parameters)
        {
            CodeMethodInvokeExpression codeMethodInvokeExpression = new CodeMethodInvokeExpression(CodeDomHelper.CreateFieldReference(null, fieldName), methodName, parameters);

            return(codeMethodInvokeExpression);
        }
        internal void AddTypeToTypeManager(CodeStatementCollection elementDictionaryStatements, CodeStatementCollection wrapperDictionaryStatements)
        {
            base.AddTypeToTypeManager(elementDictionaryStatements, "elementDictionary");
            string innerTypeFullName = null;

            if (!this.innerTypeName.Contains(this.innerTypeNs))
            {
                innerTypeFullName = string.Concat("global::", this.innerTypeNs, ".", this.innerTypeName);
            }
            CodeExpression[] codeExpressionArray = new CodeExpression[] { CodeDomHelper.Typeof(this.clrTypeInfo.clrFullTypeName), CodeDomHelper.Typeof(innerTypeFullName) };
            wrapperDictionaryStatements.Add(CodeDomHelper.CreateMethodCallFromField("wrapperDictionary", "Add", codeExpressionArray));
        }
 private void AddSetStatements(CodeStatementCollection setStatements)
 {
     setStatements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), this.wrappedFieldName), this.propertyName), CodeDomHelper.SetValue()));
 }