Inheritance: TypeMapping, INameScope
 internal MemberMapping FindDeclaringMapping(MemberMapping member, out StructMapping declaringMapping, string parent)
 {
     declaringMapping = null;
     if (this.BaseMapping != null)
     {
         MemberMapping mapping = this.BaseMapping.FindDeclaringMapping(member, out declaringMapping, parent);
         if (mapping != null)
         {
             return mapping;
         }
     }
     if (this.members != null)
     {
         for (int i = 0; i < this.members.Length; i++)
         {
             if (this.members[i].Name == member.Name)
             {
                 if (this.members[i].TypeDesc != member.TypeDesc)
                 {
                     throw new InvalidOperationException(Res.GetString("XmlHiddenMember", new object[] { parent, member.Name, member.TypeDesc.FullName, base.TypeName, this.members[i].Name, this.members[i].TypeDesc.FullName }));
                 }
                 if (!this.members[i].Match(member))
                 {
                     throw new InvalidOperationException(Res.GetString("XmlInvalidXmlOverride", new object[] { parent, member.Name, base.TypeName, this.members[i].Name }));
                 }
                 declaringMapping = this;
                 return this.members[i];
             }
         }
     }
     return null;
 }
 internal int IndexOf(StructMapping mapping)
 {
     for (int i = 0; i < this.Count; i++)
     {
         if (this[i].Mapping == mapping)
         {
             return i;
         }
     }
     return -1;
 }
 internal static void AddIncludeMetadata(CodeAttributeDeclarationCollection metadata, StructMapping mapping, Type type)
 {
     if (!mapping.IsAnonymousType)
     {
         for (StructMapping mapping2 = mapping.DerivedMappings; mapping2 != null; mapping2 = mapping2.NextDerivedMapping)
         {
             CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(type.FullName);
             declaration.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(mapping2.TypeDesc.FullName)));
             metadata.Add(declaration);
             AddIncludeMetadata(metadata, mapping2, type);
         }
     }
 }
Exemplo n.º 4
0
        private StructMapping ImportStructLikeMapping(StructModel model, RecursionLimiter limiter)
        {
            if (model.TypeDesc.Kind == TypeKind.Root)
            {
                return(this.GetRootMapping());
            }
            SoapAttributes a         = this.GetAttributes(model.Type);
            string         defaultNs = this.defaultNs;

            if ((a.SoapType != null) && (a.SoapType.Namespace != null))
            {
                defaultNs = a.SoapType.Namespace;
            }
            string        typeName    = XmlConvert.EncodeLocalName(this.XsdTypeName(model.Type, a, model.TypeDesc.Name));
            StructMapping typeMapping = (StructMapping)this.GetTypeMapping(typeName, defaultNs, model.TypeDesc);

            if (typeMapping == null)
            {
                typeMapping = new StructMapping {
                    IsSoap    = true,
                    TypeDesc  = model.TypeDesc,
                    Namespace = defaultNs,
                    TypeName  = typeName
                };
                if (a.SoapType != null)
                {
                    typeMapping.IncludeInSchema = a.SoapType.IncludeInSchema;
                }
                this.typeScope.AddTypeMapping(typeMapping);
                this.types.Add(typeName, defaultNs, typeMapping);
                if (limiter.IsExceededLimit)
                {
                    limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, typeMapping));
                    return(typeMapping);
                }
                limiter.Depth++;
                this.InitializeStructMembers(typeMapping, model, limiter);
                while (limiter.DeferredWorkItems.Count > 0)
                {
                    int index = limiter.DeferredWorkItems.Count - 1;
                    ImportStructWorkItem item = limiter.DeferredWorkItems[index];
                    if (this.InitializeStructMembers(item.Mapping, item.Model, limiter))
                    {
                        limiter.DeferredWorkItems.RemoveAt(index);
                    }
                }
                limiter.Depth--;
            }
            return(typeMapping);
        }
 internal static void AddIncludeMetadata(CodeAttributeDeclarationCollection metadata, StructMapping mapping, Type type)
 {
     if (mapping.IsAnonymousType)
     {
         return;
     }
     for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
     {
         CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(type.FullName);
         attribute.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(derived.TypeDesc.FullName)));
         metadata.Add(attribute);
         AddIncludeMetadata(metadata, derived, type);
     }
 }
Exemplo n.º 6
0
        void ExportStruct(StructMapping mapping)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                ExportRoot(mapping);
                return;
            }

            string className = mapping.TypeDesc.Name;
            string baseName  = mapping.TypeDesc.BaseTypeDesc == null ? string.Empty : mapping.TypeDesc.BaseTypeDesc.Name;

            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);

            codeClass.CustomAttributes = new CodeAttributeDeclarationCollection();
            codeClass.Comments.Add(new CodeCommentStatement("<remarks/>", true));
            codeNamespace.Types.Add(codeClass);

            if (baseName != null && baseName.Length > 0)
            {
                codeClass.BaseTypes.Add(baseName);
            }

            codeClass.TypeAttributes |= TypeAttributes.Public;
            if (mapping.TypeDesc.IsAbstract)
            {
                codeClass.TypeAttributes |= TypeAttributes.Abstract;
            }

            AddTypeMetadata(codeClass.CustomAttributes, mapping.TypeName, mapping.Namespace, mapping.IncludeInSchema);
            AddIncludeMetadata(codeClass.CustomAttributes, mapping);

            for (int i = 0; i < mapping.Members.Length; i++)
            {
                ExportMember(codeClass, mapping.Members[i]);
            }

            for (int i = 0; i < mapping.Members.Length; i++)
            {
                EnsureTypesExported(mapping.Members[i].Elements);
            }

            if (mapping.BaseMapping != null)
            {
                ExportType(mapping.BaseMapping);
            }

            ExportDerivedStructs(mapping);
        }
 internal void SetSequence()
 {
     if (!base.TypeDesc.IsRoot)
     {
         StructMapping baseMapping = this;
         while ((!baseMapping.BaseMapping.IsSequence && (baseMapping.BaseMapping != null)) && !baseMapping.BaseMapping.TypeDesc.IsRoot)
         {
             baseMapping = baseMapping.BaseMapping;
         }
         baseMapping.IsSequence = true;
         for (StructMapping mapping2 = baseMapping.DerivedMappings; mapping2 != null; mapping2 = mapping2.NextDerivedMapping)
         {
             mapping2.SetSequence();
         }
     }
 }
Exemplo n.º 8
0
        void ExportRoot(StructMapping mapping)
        {
            if (!rootExported)
            {
                rootExported = true;
                ExportDerivedStructs(mapping);

                for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
                {
                    if (!derived.ReferencedByElement)
                    {
                        CodeIdentifier.CheckValidTypeIdentifier(derived.TypeDesc.FullName);
                        CodeAttributeArgument[] arguments = new CodeAttributeArgument[] { new CodeAttributeArgument(new CodeTypeOfExpression(derived.TypeDesc.FullName)) };
                        AddCustomAttribute(includeMetadata, typeof(SoapIncludeAttribute), arguments);
                    }
                }
            }
        }
Exemplo n.º 9
0
        bool WriteDerivedTypes(out object o, StructMapping mapping, XmlQualifiedName xsiType, string defaultNamespace, bool checkType, bool isNullable)
        {
            for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
            {
                if (QNameEqual(xsiType, derived.TypeName, derived.Namespace, defaultNamespace))
                {
                    o = WriteStructMethod(derived, isNullable, checkType, defaultNamespace);
                    return(true);
                }

                if (WriteDerivedTypes(out o, derived, xsiType, defaultNamespace, checkType, isNullable))
                {
                    return(true);
                }
            }

            o = null;
            return(false);
        }
Exemplo n.º 10
0
        bool WriteDerivedTypes(StructMapping mapping, string n, string ns, object o, bool isNullable)
        {
            Type t = o.GetType();

            for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
            {
                if (t == derived.TypeDesc.Type)
                {
                    WriteStructMethod(derived, n, ns, o, isNullable, needType: true);
                    return(true);
                }

                if (WriteDerivedTypes(derived, n, ns, o, isNullable))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 11
0
        private static void GetSettableMembers(StructMapping mapping, ArrayList list)
        {
            if (mapping.BaseMapping != null)
            {
                GetSettableMembers(mapping.BaseMapping, list);
            }

            if (mapping.Members != null)
            {
                foreach (MemberMapping memberMapping in mapping.Members)
                {
                    MemberInfo   memberInfo   = memberMapping.MemberInfo;
                    PropertyInfo propertyInfo = memberInfo as PropertyInfo;
                    if (propertyInfo != null && !CanWriteProperty(propertyInfo, memberMapping.TypeDesc))
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlReadOnlyPropertyError, propertyInfo.DeclaringType, propertyInfo.Name));
                    }
                    list.Add(memberMapping);
                }
            }
        }
Exemplo n.º 12
0
        internal void SetSequence()
        {
            if (TypeDesc.IsRoot)
            {
                return;
            }

            StructMapping start = this;

            // find first mapping that does not have the sequence set
            while (start.BaseMapping != null && !start.BaseMapping.IsSequence && !start.BaseMapping.TypeDesc.IsRoot)
            {
                start = start.BaseMapping;
            }

            start.IsSequence = true;
            for (StructMapping derived = start.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
            {
                derived.SetSequence();
            }
        }
        private XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                return(this.ExportRootMapping(mapping));
            }
            XmlSchemaComplexType type = (XmlSchemaComplexType)this.types[mapping];

            if (type == null)
            {
                if (!mapping.IncludeInSchema)
                {
                    throw new InvalidOperationException(Res.GetString("XmlSoapCannotIncludeInSchema", new object[] { mapping.TypeDesc.Name }));
                }
                this.CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
                type = new XmlSchemaComplexType {
                    Name = mapping.TypeName
                };
                this.types.Add(mapping, type);
                this.AddSchemaItem(type, mapping.Namespace, ns);
                type.IsAbstract = mapping.TypeDesc.IsAbstract;
                if ((mapping.BaseMapping != null) && mapping.BaseMapping.IncludeInSchema)
                {
                    XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension {
                        BaseTypeName = this.ExportStructMapping(mapping.BaseMapping, mapping.Namespace)
                    };
                    XmlSchemaComplexContent content = new XmlSchemaComplexContent {
                        Content = extension
                    };
                    type.ContentModel = content;
                }
                this.ExportTypeMembers(type, mapping.Members, mapping.Namespace);
                this.ExportDerivedMappings(mapping);
            }
            else
            {
                this.AddSchemaImport(mapping.Namespace, ns);
            }
            return(new XmlQualifiedName(type.Name, mapping.Namespace));
        }
        XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                return(ExportRootMapping(mapping));
            }
            XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping];

            if (type == null)
            {
                if (!mapping.IncludeInSchema)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlSoapCannotIncludeInSchema, mapping.TypeDesc.Name));
                }
                CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
                type      = new XmlSchemaComplexType();
                type.Name = mapping.TypeName;
                types.Add(mapping, type);
                AddSchemaItem(type, mapping.Namespace, ns);
                type.IsAbstract = mapping.TypeDesc.IsAbstract;

                if (mapping.BaseMapping != null && mapping.BaseMapping.IncludeInSchema)
                {
                    XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
                    extension.BaseTypeName = ExportStructMapping(mapping.BaseMapping, mapping.Namespace);
                    XmlSchemaComplexContent model = new XmlSchemaComplexContent();
                    model.Content     = extension;
                    type.ContentModel = model;
                }
                ExportTypeMembers(type, mapping.Members, mapping.Namespace);
                ExportDerivedMappings(mapping);
            }
            else
            {
                AddSchemaImport(mapping.Namespace, ns);
            }
            return(new XmlQualifiedName(type.Name, mapping.Namespace));
        }
Exemplo n.º 15
0
        private void WriteMappingInfo(TypeMapping mapping, string typeVariable, Type type)
        {
            string cSharpName = mapping.TypeDesc.CSharpName;

            if (mapping is StructMapping)
            {
                StructMapping mapping2 = mapping as StructMapping;
                for (int i = 0; i < mapping2.Members.Length; i++)
                {
                    MemberMapping mapping3 = mapping2.Members[i];
                    this.WriteMemberInfo(type, cSharpName, typeVariable, mapping3.Name);
                    if (mapping3.CheckShouldPersist)
                    {
                        string memberName = "ShouldSerialize" + mapping3.Name;
                        this.WriteMethodInfo(cSharpName, typeVariable, memberName, false, new string[0]);
                    }
                    if (mapping3.CheckSpecified != SpecifiedAccessor.None)
                    {
                        string str3 = mapping3.Name + "Specified";
                        this.WriteMemberInfo(type, cSharpName, typeVariable, str3);
                    }
                    if (mapping3.ChoiceIdentifier != null)
                    {
                        string str4 = mapping3.ChoiceIdentifier.MemberName;
                        this.WriteMemberInfo(type, cSharpName, typeVariable, str4);
                    }
                }
            }
            else if (mapping is EnumMapping)
            {
                FieldInfo[] fields = type.GetFields();
                for (int j = 0; j < fields.Length; j++)
                {
                    this.WriteMemberInfo(type, cSharpName, typeVariable, fields[j].Name);
                }
            }
        }
Exemplo n.º 16
0
 internal void MakeDerived(StructMapping structMapping, Type baseType, bool baseTypeCanBeIndirect)
 {
     structMapping.ReferencedByTopLevelElement = true;
     if (baseType != null)
     {
         TypeDesc typeDesc = this.Scope.GetTypeDesc(baseType);
         if (typeDesc != null)
         {
             TypeDesc baseTypeDesc = structMapping.TypeDesc;
             if (baseTypeCanBeIndirect)
             {
                 while ((baseTypeDesc.BaseTypeDesc != null) && (baseTypeDesc.BaseTypeDesc != typeDesc))
                 {
                     baseTypeDesc = baseTypeDesc.BaseTypeDesc;
                 }
             }
             if ((baseTypeDesc.BaseTypeDesc != null) && (baseTypeDesc.BaseTypeDesc != typeDesc))
             {
                 throw new InvalidOperationException(Res.GetString("XmlInvalidBaseType", new object[] { structMapping.TypeDesc.FullName, baseType.FullName, baseTypeDesc.BaseTypeDesc.FullName }));
             }
             baseTypeDesc.BaseTypeDesc = typeDesc;
         }
     }
 }
Exemplo n.º 17
0
 internal static MemberMapping[] GetSettableMembers(StructMapping structMapping)
 {
     ArrayList list = new ArrayList();
     GetSettableMembers(structMapping, list);
     return (MemberMapping[])list.ToArray(typeof(MemberMapping));
 }
Exemplo n.º 18
0
 internal static MemberMapping[] GetAllMembers(StructMapping mapping)
 {
     if (mapping.BaseMapping == null)
         return mapping.Members;
     ArrayList list = new ArrayList();
     GetAllMembers(mapping, list);
     return (MemberMapping[])list.ToArray(typeof(MemberMapping));
 }
        void WriteLiteralStructMethod(StructMapping structMapping) {
            string methodName = (string)MethodNames[structMapping];
            bool useReflection = structMapping.TypeDesc.UseReflection;
            string typeName = useReflection ? "object" : structMapping.TypeDesc.CSharpName;
            Writer.WriteLine();
            Writer.Write(typeName);
            Writer.Write(" ");
            Writer.Write(methodName);
            Writer.Write("(");
            if (structMapping.TypeDesc.IsNullable)
                Writer.Write("bool isNullable, ");
            Writer.WriteLine("bool checkType) {");
            Writer.Indent++;

            Writer.Write(typeof(XmlQualifiedName).FullName);
            Writer.WriteLine(" xsiType = checkType ? GetXsiType() : null;");
            Writer.WriteLine("bool isNull = false;");
            if (structMapping.TypeDesc.IsNullable)
                Writer.WriteLine("if (isNullable) isNull = ReadNull();");

            Writer.WriteLine("if (checkType) {");
            if (structMapping.TypeDesc.IsRoot) {
                Writer.Indent++;
                Writer.WriteLine("if (isNull) {"); 
                Writer.Indent++;
                Writer.WriteLine("if (xsiType != null) return (" + typeName + ")ReadTypedNull(xsiType);"); 
                Writer.Write("else return ");
                if (structMapping.TypeDesc.IsValueType) {
                    Writer.Write(RaCodeGen.GetStringForCreateInstance(structMapping.TypeDesc.CSharpName, useReflection, false, false));
                    Writer.WriteLine(";");
                }
                else
                    Writer.WriteLine("null;");

                Writer.Indent--;
                Writer.WriteLine("}"); 
            }
            Writer.Write("if (xsiType == null");
            if (!structMapping.TypeDesc.IsRoot) {
                Writer.Write(" || ");
                WriteQNameEqual("xsiType", structMapping.TypeName, structMapping.Namespace);
            }
            Writer.WriteLine(") {");
            if (structMapping.TypeDesc.IsRoot) {
                Writer.Indent++;
                Writer.WriteLine("return ReadTypedPrimitive(new System.Xml.XmlQualifiedName(\"" + Soap.UrType + "\", \"" + XmlSchema.Namespace + "\"));");
                Writer.Indent--;
            }
            Writer.WriteLine("}");
            WriteDerivedTypes(structMapping, !useReflection && !structMapping.TypeDesc.IsRoot, typeName);
            if (structMapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes();
            Writer.WriteLine("else");
            Writer.Indent++;
            if (structMapping.TypeDesc.IsRoot)
                Writer.Write("return ReadTypedPrimitive((");
            else
                Writer.Write("throw CreateUnknownTypeException((");
            Writer.Write(typeof(XmlQualifiedName).FullName);
            Writer.WriteLine(")xsiType);");
            Writer.Indent--;
            Writer.WriteLine("}");

            if (structMapping.TypeDesc.IsNullable)
                Writer.WriteLine("if (isNull) return null;");

            if (structMapping.TypeDesc.IsAbstract) {
                Writer.Write("throw CreateAbstractTypeException(");
                WriteQuotedCSharpString(structMapping.TypeName);
                Writer.Write(", ");
                WriteQuotedCSharpString(structMapping.Namespace);
                Writer.WriteLine(");");
            }
            else {
                if (structMapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(structMapping.TypeDesc.Type)) {
                    Writer.WriteLine("DecodeName = false;");
                }
                WriteCreateMapping(structMapping, "o");
                
                MemberMapping[] mappings = TypeScope.GetSettableMembers(structMapping);
                
                Member anyText = null;
                Member anyElement = null;
                Member anyAttribute = null; 
                bool isSequence = structMapping.HasExplicitSequence();

                ArrayList arraysToDeclareList = new ArrayList(mappings.Length);
                ArrayList arraysToSetList = new ArrayList(mappings.Length);
                ArrayList allMembersList = new ArrayList(mappings.Length);

                for (int i = 0; i < mappings.Length; i++) {
                    MemberMapping mapping = mappings[i];
                    CodeIdentifier.CheckValidIdentifier(mapping.Name);
                    string source = RaCodeGen.GetStringForMember("o", mapping.Name, structMapping.TypeDesc);
                    Member member = new Member(this, source, "a", i, mapping, GetChoiceIdentifierSource(mapping, "o", structMapping.TypeDesc));
                    if (!mapping.IsSequence)
                        member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
                    member.IsNullable = mapping.TypeDesc.IsNullable;
                    if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite)
                        member.CheckSpecifiedSource = RaCodeGen.GetStringForMember("o", mapping.Name + "Specified", structMapping.TypeDesc);
                    if (mapping.Text != null)
                        anyText = member;
                    if (mapping.Attribute != null && mapping.Attribute.Any)
                        anyAttribute = member;
                    if (!isSequence) {
                        // find anyElement if present.
                        for (int j = 0; j < mapping.Elements.Length; j++) {
                            if (mapping.Elements[j].Any && (mapping.Elements[j].Name == null || mapping.Elements[j].Name.Length == 0)) {
                                anyElement = member;
                                break;
                            }
                        }
                    }
                    else if (mapping.IsParticle && !mapping.IsSequence) {
                        StructMapping declaringMapping;
                        structMapping.FindDeclaringMapping(mapping, out declaringMapping, structMapping.TypeName);
                        throw new InvalidOperationException(Res.GetString(Res.XmlSequenceHierarchy, structMapping.TypeDesc.FullName, mapping.Name, declaringMapping.TypeDesc.FullName, "Order"));
                    }
                    if (mapping.Attribute == null && mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping) {
                        Member arrayMember = new Member(this, source, source, "a", i, mapping, GetChoiceIdentifierSource(mapping, "o", structMapping.TypeDesc));
                        arrayMember.CheckSpecifiedSource = member.CheckSpecifiedSource;
                        allMembersList.Add(arrayMember);
                    }
                    else {
                        allMembersList.Add(member);
                    }

                    if (mapping.TypeDesc.IsArrayLike) {
                        arraysToDeclareList.Add(member);
                        if (mapping.TypeDesc.IsArrayLike && !(mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping)) {
                            member.ParamsReadSource = null; // flat arrays -- don't want to count params read.
                            if (member != anyText && member != anyElement) {
                                arraysToSetList.Add(member);
                            }
                        }
                        else if (!mapping.TypeDesc.IsArray) {
                            member.ParamsReadSource = null; // collection
                        }
                    }
                }
                if (anyElement != null) arraysToSetList.Add(anyElement);
                if (anyText != null && anyText != anyElement) arraysToSetList.Add(anyText);

                Member[] arraysToDeclare = (Member[]) arraysToDeclareList.ToArray(typeof(Member));
                Member[] arraysToSet = (Member[]) arraysToSetList.ToArray(typeof(Member));
                Member[] allMembers = (Member[]) allMembersList.ToArray(typeof(Member));

                WriteMemberBegin(arraysToDeclare);
                WriteParamsRead(mappings.Length);

                WriteAttributes(allMembers, anyAttribute, "UnknownNode", "(object)o");
                if (anyAttribute != null)
                    WriteMemberEnd(arraysToDeclare);

                Writer.WriteLine("Reader.MoveToElement();");

                Writer.WriteLine("if (Reader.IsEmptyElement) {");
                Writer.Indent++;
                Writer.WriteLine("Reader.Skip();");
                WriteMemberEnd(arraysToSet);
                Writer.WriteLine("return o;");
                Writer.Indent--;
                Writer.WriteLine("}");

                Writer.WriteLine("Reader.ReadStartElement();");
                if (IsSequence(allMembers)) {
                    Writer.WriteLine("int state = 0;");
                }
                int loopIndex = WriteWhileNotLoopStart();
                Writer.Indent++;
                string unknownNode = "UnknownNode((object)o, " + ExpectedElements(allMembers) + ");";
                WriteMemberElements(allMembers, unknownNode, unknownNode, anyElement, anyText, null);
                Writer.WriteLine("Reader.MoveToContent();");

                WriteWhileLoopEnd(loopIndex);
                WriteMemberEnd(arraysToSet);

                Writer.WriteLine("ReadEndElement();");
                Writer.WriteLine("return o;");
            }
            Writer.Indent--;
            Writer.WriteLine("}");
        }        
        void WriteDerivedTypes(StructMapping mapping, bool isTypedReturn, string returnTypeName) {

            for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
                Writer.Write("else if (");
                WriteQNameEqual("xsiType", derived.TypeName, derived.Namespace);
                Writer.WriteLine(")");
                Writer.Indent++;

                string methodName = ReferenceMapping(derived);
                #if DEBUG
                    // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                    if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, derived.TypeDesc.Name));
                #endif

                Writer.Write("return ");
                if (derived.TypeDesc.UseReflection && isTypedReturn)
                    Writer.Write("(" + returnTypeName + ")");
                Writer.Write(methodName);
                Writer.Write("(");
                if (derived.TypeDesc.IsNullable)
                    Writer.Write("isNullable, ");
                Writer.WriteLine("false);");

                Writer.Indent--;

                WriteDerivedTypes(derived, isTypedReturn, returnTypeName);
            }
        }
Exemplo n.º 21
0
        private bool InitializeStructMembers(StructMapping mapping, StructModel model, RecursionLimiter limiter)
        {
            if (mapping.IsFullyInitialized)
            {
                return(true);
            }
            if (model.TypeDesc.BaseTypeDesc != null)
            {
                StructMapping baseMapping = ImportStructLikeMapping((StructModel)_modelScope.GetTypeModel(model.Type.BaseType, false), limiter);

                // check to see if the import of the baseMapping was deferred
                int baseIndex = limiter.DeferredWorkItems.IndexOf(mapping.BaseMapping);
                if (baseIndex < 0)
                {
                    mapping.BaseMapping = baseMapping;
                }
                else
                {
                    // the import of the baseMapping was deferred, make sure that the derived mappings is deferred as well
                    if (!limiter.DeferredWorkItems.Contains(mapping))
                    {
                        limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    }
                    // make sure that baseMapping get processed before the derived
                    int top = limiter.DeferredWorkItems.Count - 1;
                    if (baseIndex < top)
                    {
                        ImportStructWorkItem baseMappingWorkItem = limiter.DeferredWorkItems[baseIndex];
                        limiter.DeferredWorkItems[baseIndex] = limiter.DeferredWorkItems[top];
                        limiter.DeferredWorkItems[top]       = baseMappingWorkItem;
                    }
                    return(false);
                }
            }
            ArrayList members = new ArrayList();

            foreach (MemberInfo memberInfo in model.GetMemberInfos())
            {
                if (!(memberInfo is FieldInfo) && !(memberInfo is PropertyInfo))
                {
                    continue;
                }
                SoapAttributes memberAttrs = GetAttributes(memberInfo);
                if (memberAttrs.SoapIgnore)
                {
                    continue;
                }
                FieldModel fieldModel = model.GetFieldModel(memberInfo);
                if (fieldModel == null)
                {
                    continue;
                }
                MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace, limiter);
                if (member == null)
                {
                    continue;
                }

                if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum && !member.TypeDesc.IsOptionalValue)
                {
                    if (model.TypeDesc.IsValueType)
                    {
                        throw new NotSupportedException(SR.Format(SR.XmlRpcRefsInValueType, model.TypeDesc.FullName));
                    }
                    if (member.TypeDesc.IsValueType)
                    {
                        throw new NotSupportedException(SR.Format(SR.XmlRpcNestedValueType, member.TypeDesc.FullName));
                    }
                }
                if (mapping.BaseMapping != null)
                {
                    if (mapping.BaseMapping.Declares(member, mapping.TypeName))
                    {
                        continue;
                    }
                }
                members.Add(member);
            }
            mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));
            if (mapping.BaseMapping == null)
            {
                mapping.BaseMapping = GetRootMapping();
            }
            IncludeTypes(model.Type, limiter);

            return(true);
        }
 StructMapping GetRootMapping() {
     if (root == null) {
         root = CreateRootMapping();
         typeScope.AddTypeMapping(root);
     }
     return root;
 }
Exemplo n.º 23
0
 internal abstract void ExportDerivedStructs(StructMapping mapping);
Exemplo n.º 24
0
 internal bool Contains(StructMapping mapping)
 {
     return(this.IndexOf(mapping) >= 0);
 }
Exemplo n.º 25
0
        private void WriteStructMethod(StructMapping mapping, string n, string ns, object o, bool isNullable, bool needType, XmlMapping parentMapping = null)
        {
            if (mapping.IsSoap && mapping.TypeDesc.IsRoot)
            {
                return;
            }

            if (mapping.IsSoap)
            {
            }
            else
            {
                if (o == null)
                {
                    if (isNullable)
                    {
                        WriteNullTagLiteral(n, ns);
                    }
                    return;
                }

                if (!needType &&
                    o.GetType() != mapping.TypeDesc.Type)
                {
                    if (WriteDerivedTypes(mapping, n, ns, o, isNullable))
                    {
                        return;
                    }

                    if (mapping.TypeDesc.IsRoot)
                    {
                        if (WriteEnumAndArrayTypes(mapping, o, n, ns, parentMapping))
                        {
                            return;
                        }

                        WriteTypedPrimitive(n, ns, o, true);
                        return;
                    }

                    throw CreateUnknownTypeException(o);
                }
            }

            if (!mapping.TypeDesc.IsAbstract)
            {
                if (mapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(mapping.TypeDesc.Type))
                {
                    EscapeName = false;
                }

                XmlSerializerNamespaces xmlnsSource = null;
                MemberMapping[]         members     = TypeScope.GetAllMembers(mapping);
                int xmlnsMember = FindXmlnsIndex(members);
                if (xmlnsMember >= 0)
                {
                    MemberMapping member = members[xmlnsMember];
                    xmlnsSource = (XmlSerializerNamespaces)GetMemberValue(o, member.Name);
                }

                if (!mapping.IsSoap)
                {
                    WriteStartElement(n, ns, o, false, xmlnsSource);

                    if (!mapping.TypeDesc.IsRoot)
                    {
                        if (needType)
                        {
                            WriteXsiType(mapping.TypeName, mapping.Namespace);
                        }
                    }
                }
                else if (xmlnsSource != null)
                {
                    WriteNamespaceDeclarations(xmlnsSource);
                }

                for (int i = 0; i < members.Length; i++)
                {
                    MemberMapping m           = members[i];
                    string        memberName  = m.Name;
                    object        memberValue = GetMemberValue(o, memberName);

                    bool isSpecified   = true;
                    bool shouldPersist = true;
                    if (m.CheckSpecified != SpecifiedAccessor.None)
                    {
                        string specifiedMemberName = m.Name + "Specified";
                        isSpecified = (bool)GetMemberValue(o, specifiedMemberName);
                    }

                    if (m.CheckShouldPersist)
                    {
                        string     methodInvoke = "ShouldSerialize" + m.Name;
                        MethodInfo method       = o.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke);
                        shouldPersist = (bool)method.Invoke(o, Array.Empty <object>());
                    }

                    if (m.Attribute != null)
                    {
                        if (isSpecified && shouldPersist)
                        {
                            WriteMember(memberValue, m.Attribute, m.TypeDesc, o);
                        }
                    }
                }

                for (int i = 0; i < members.Length; i++)
                {
                    MemberMapping m           = members[i];
                    string        memberName  = m.Name;
                    object        memberValue = GetMemberValue(o, memberName);

                    bool isSpecified   = true;
                    bool shouldPersist = true;
                    if (m.CheckSpecified != SpecifiedAccessor.None)
                    {
                        string specifiedMemberName = m.Name + "Specified";
                        isSpecified = (bool)GetMemberValue(o, specifiedMemberName);
                    }

                    if (m.CheckShouldPersist)
                    {
                        string     methodInvoke = "ShouldSerialize" + m.Name;
                        MethodInfo method       = o.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke);
                        shouldPersist = (bool)method.Invoke(o, Array.Empty <object>());
                    }

                    if (m.Xmlns != null)
                    {
                        continue;
                    }

                    bool checkShouldPersist = m.CheckShouldPersist && (m.Elements.Length > 0 || m.Text != null);

                    if (!checkShouldPersist)
                    {
                        shouldPersist = true;
                    }

                    object choiceSource = null;
                    if (m.ChoiceIdentifier != null)
                    {
                        choiceSource = GetMemberValue(o, m.ChoiceIdentifier.MemberName);
                    }

                    if (isSpecified && shouldPersist)
                    {
                        WriteMember(memberValue, choiceSource, m.ElementsSortedByDerivation, m.Text, m.ChoiceIdentifier, m.TypeDesc, true, parentMapping);
                    }
                }
                if (!mapping.IsSoap)
                {
                    WriteEndElement(o);
                }
            }
        }
Exemplo n.º 26
0
        StructMapping ImportStructType(XmlSchemaComplexType type, string typeNs, bool excludeFromImport)
        {
            if (type.Name == null)
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSchemaType));
            }

            TypeDesc baseTypeDesc = null;

            Mapping baseMapping = null;

            if (!type.DerivedFrom.IsEmpty)
            {
                baseMapping = ImportType(type.DerivedFrom, excludeFromImport);

                if (baseMapping is StructMapping)
                {
                    baseTypeDesc = ((StructMapping)baseMapping).TypeDesc;
                }
                else
                {
                    baseMapping = null;
                }
            }
            if (baseMapping == null)
            {
                baseMapping = GetRootMapping();
            }
            Mapping previousMapping = (Mapping)mappings[type];

            if (previousMapping != null)
            {
                return((StructMapping)previousMapping);
            }
            string        typeName      = GenerateUniqueTypeName(type.Name);
            StructMapping structMapping = new StructMapping();
            TypeFlags     flags         = TypeFlags.Reference;

            if (type.IsAbstract)
            {
                flags |= TypeFlags.Abstract;
            }
            structMapping.TypeDesc  = new TypeDesc(typeName, typeName, TypeKind.Struct, baseTypeDesc, flags);
            structMapping.Namespace = typeNs;
            structMapping.TypeName  = type.Name;
            if (!excludeFromImport)
            {
                structMapping.BaseMapping = (StructMapping)baseMapping;
                mappings.Add(type, structMapping);
            }
            CodeIdentifiers members = new CodeIdentifiers();

            members.AddReserved(typeName);
            structMapping.Members = ImportTypeMembers(type, typeNs, members);
            if (!excludeFromImport)
            {
                scope.AddTypeMapping(structMapping);
            }
            ImportDerivedTypes(new XmlQualifiedName(type.Name, typeNs));
            return(structMapping);
        }
 internal ImportStructWorkItem(StructModel model, StructMapping mapping)
 {
     this.model   = model;
     this.mapping = mapping;
 }
        private CodeTypeDeclaration ExportStruct(StructMapping mapping)
        {
            CodeConstructor constructor;

            if (mapping.TypeDesc.IsRoot)
            {
                base.ExportRoot(mapping, typeof(XmlIncludeAttribute));
                return(null);
            }
            string name = mapping.TypeDesc.Name;
            string str2 = ((mapping.TypeDesc.BaseTypeDesc == null) || mapping.TypeDesc.BaseTypeDesc.IsRoot) ? string.Empty : mapping.TypeDesc.BaseTypeDesc.FullName;
            CodeTypeDeclaration declaration = new CodeTypeDeclaration(name)
            {
                IsPartial = base.CodeProvider.Supports(GeneratorSupport.PartialTypes)
            };

            declaration.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true));
            base.CodeNamespace.Types.Add(declaration);
            constructor = new CodeConstructor {
                Attributes = (constructor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public
            };
            declaration.Members.Add(constructor);
            if (mapping.TypeDesc.IsAbstract)
            {
                constructor.Attributes |= MemberAttributes.Abstract;
            }
            if ((str2 != null) && (str2.Length > 0))
            {
                declaration.BaseTypes.Add(str2);
            }
            else
            {
                base.AddPropertyChangedNotifier(declaration);
            }
            declaration.TypeAttributes |= TypeAttributes.Public;
            if (mapping.TypeDesc.IsAbstract)
            {
                declaration.TypeAttributes |= TypeAttributes.Abstract;
            }
            CodeExporter.AddIncludeMetadata(declaration.CustomAttributes, mapping, typeof(XmlIncludeAttribute));
            if (mapping.IsSequence)
            {
                int num = 0;
                for (int j = 0; j < mapping.Members.Length; j++)
                {
                    MemberMapping mapping2 = mapping.Members[j];
                    if (mapping2.IsParticle && (mapping2.SequenceId < 0))
                    {
                        mapping2.SequenceId = num++;
                    }
                }
            }
            if (base.GenerateProperties)
            {
                for (int k = 0; k < mapping.Members.Length; k++)
                {
                    this.ExportProperty(declaration, mapping.Members[k], mapping.Namespace, mapping.Scope, constructor);
                }
            }
            else
            {
                for (int m = 0; m < mapping.Members.Length; m++)
                {
                    this.ExportMember(declaration, mapping.Members[m], mapping.Namespace, constructor);
                }
            }
            for (int i = 0; i < mapping.Members.Length; i++)
            {
                if (mapping.Members[i].Xmlns == null)
                {
                    this.EnsureTypesExported(mapping.Members[i].Elements, mapping.Namespace);
                    this.EnsureTypesExported(mapping.Members[i].Attribute, mapping.Namespace);
                    this.EnsureTypesExported(mapping.Members[i].Text, mapping.Namespace);
                }
            }
            if (mapping.BaseMapping != null)
            {
                this.ExportType(mapping.BaseMapping, null, mapping.Namespace, null, false);
            }
            this.ExportDerivedStructs(mapping);
            CodeGenerator.ValidateIdentifiers(declaration);
            if (constructor.Statements.Count == 0)
            {
                declaration.Members.Remove(constructor);
            }
            return(declaration);
        }
Exemplo n.º 29
0
        private StructMapping ImportStructType(XmlSchemaComplexType type, string typeNs, bool excludeFromImport)
        {
            if (type.Name == null)
            {
                XmlSchemaElement parent     = (XmlSchemaElement)type.Parent;
                XmlQualifiedName parentName = XmlSchemas.GetParentName(parent);
                throw new InvalidOperationException(Res.GetString("XmlInvalidSchemaElementType", new object[] { parentName.Name, parentName.Namespace, parent.Name }));
            }
            TypeDesc baseTypeDesc = null;
            Mapping  rootMapping  = null;

            if (!type.DerivedFrom.IsEmpty)
            {
                rootMapping = this.ImportType(type.DerivedFrom, excludeFromImport);
                if (rootMapping is StructMapping)
                {
                    baseTypeDesc = ((StructMapping)rootMapping).TypeDesc;
                }
                else
                {
                    rootMapping = null;
                }
            }
            if (rootMapping == null)
            {
                rootMapping = base.GetRootMapping();
            }
            Mapping mapping2 = (Mapping)base.ImportedMappings[type];

            if (mapping2 != null)
            {
                return((StructMapping)mapping2);
            }
            string        str      = base.GenerateUniqueTypeName(Accessor.UnescapeName(type.Name));
            StructMapping mapping3 = new StructMapping {
                IsReference = base.Schemas.IsReference(type)
            };
            TypeFlags reference = TypeFlags.Reference;

            if (type.IsAbstract)
            {
                reference |= TypeFlags.Abstract;
            }
            mapping3.TypeDesc    = new TypeDesc(str, str, TypeKind.Struct, baseTypeDesc, reference);
            mapping3.Namespace   = typeNs;
            mapping3.TypeName    = type.Name;
            mapping3.BaseMapping = (StructMapping)rootMapping;
            base.ImportedMappings.Add(type, mapping3);
            if (excludeFromImport)
            {
                mapping3.IncludeInSchema = false;
            }
            CodeIdentifiers scope = new CodeIdentifiers();

            scope.AddReserved(str);
            base.AddReservedIdentifiersForDataBinding(scope);
            mapping3.Members = this.ImportTypeMembers(type, typeNs, scope);
            base.Scope.AddTypeMapping(mapping3);
            this.ImportDerivedTypes(new XmlQualifiedName(type.Name, typeNs));
            return(mapping3);
        }
Exemplo n.º 30
0
 internal static MemberMapping[] GetSettableMembers(StructMapping mapping, System.Collections.Generic.Dictionary<string, MemberInfo> memberInfos)
 {
     MemberMapping[] mappings = GetSettableMembers(mapping);
     PopulateMemberInfos(mapping, mappings, memberInfos);
     return mappings;
 }
Exemplo n.º 31
0
        internal MemberMapping FindDeclaringMapping(MemberMapping member, out StructMapping declaringMapping, string parent)
        {
            declaringMapping = null;
            if (BaseMapping != null)
            {
                MemberMapping baseMember = BaseMapping.FindDeclaringMapping(member, out declaringMapping, parent);
                if (baseMember != null) return baseMember;
            }
            if (_members == null) return null;

            for (int i = 0; i < _members.Length; i++)
            {
                if (_members[i].Name == member.Name)
                {
                    if (_members[i].TypeDesc != member.TypeDesc)
                        throw new InvalidOperationException(SR.Format(SR.XmlHiddenMember, parent, member.Name, member.TypeDesc.FullName, this.TypeName, _members[i].Name, _members[i].TypeDesc.FullName));
                    else if (!_members[i].Match(member))
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlInvalidXmlOverride, parent, member.Name, this.TypeName, _members[i].Name));
                    }
                    declaringMapping = this;
                    return _members[i];
                }
            }
            return null;
        }
Exemplo n.º 32
0
 internal void MakeDerived(StructMapping structMapping, Type baseType, bool baseTypeCanBeIndirect)
 {
     structMapping.ReferencedByTopLevelElement = true;
     TypeDesc baseTypeDesc;
     if (baseType != null)
     {
         baseTypeDesc = Scope.GetTypeDesc(baseType);
         if (baseTypeDesc != null)
         {
             TypeDesc typeDescToChange = structMapping.TypeDesc;
             if (baseTypeCanBeIndirect)
             {
                 // if baseTypeCanBeIndirect is true, we apply the supplied baseType to the top of the
                 // inheritance chain, not necessarily directly to the imported type.
                 while (typeDescToChange.BaseTypeDesc != null && typeDescToChange.BaseTypeDesc != baseTypeDesc)
                     typeDescToChange = typeDescToChange.BaseTypeDesc;
             }
             if (typeDescToChange.BaseTypeDesc != null && typeDescToChange.BaseTypeDesc != baseTypeDesc)
                 throw new InvalidOperationException(SR.Format(SR.XmlInvalidBaseType, structMapping.TypeDesc.FullName, baseType.FullName, typeDescToChange.BaseTypeDesc.FullName));
             typeDescToChange.BaseTypeDesc = baseTypeDesc;
         }
     }
 }
        StructMapping ImportStructLikeMapping(StructModel model)
        {
            if (model.TypeDesc.Kind == TypeKind.Root)
            {
                return(GetRootMapping());
            }

            SoapAttributes a = GetAttributes(model.Type);

            string typeNs = defaultNs;

            if (a.SoapType != null && a.SoapType.Namespace != null)
            {
                typeNs = a.SoapType.Namespace;
            }

            string typeName = string.Empty;

            if (a.SoapType != null)
            {
                typeName = a.SoapType.TypeName;
            }
            if (typeName.Length == 0)
            {
                typeName = model.TypeDesc.Name;
            }


            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);

            if (mapping == null)
            {
                mapping           = new StructMapping();
                mapping.IsSoap    = true;
                mapping.TypeDesc  = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName  = typeName;
                if (a.SoapType != null)
                {
                    mapping.IncludeInSchema = a.SoapType.IncludeInSchema;
                }
                typeScope.AddTypeMapping(mapping);
                types.Add(typeName, typeNs, mapping);
                if (model.TypeDesc.BaseTypeDesc != null)
                {
                    mapping.BaseMapping = ImportStructLikeMapping((StructModel)modelScope.GetTypeModel(model.Type.BaseType, false));
                }
                ArrayList members = new ArrayList();
                foreach (MemberInfo memberInfo in model.GetMemberInfos())
                {
                    SoapAttributes memberAttrs = GetAttributes(memberInfo);
                    if (memberAttrs.SoapIgnore)
                    {
                        continue;
                    }
                    FieldModel fieldModel = model.GetFieldModel(memberInfo);
                    if (fieldModel == null)
                    {
                        continue;
                    }
                    MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace);
                    if (member == null)
                    {
                        continue;
                    }

                    if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum)
                    {
                        if (model.TypeDesc.IsValueType)
                        {
                            throw new NotSupportedException(Res.GetString(Res.XmlRpcRefsInValueType, model.TypeDesc.FullName));
                        }
                        if (member.TypeDesc.IsValueType)
                        {
                            throw new NotSupportedException(Res.GetString(Res.XmlRpcNestedValueType, member.TypeDesc.FullName));
                        }
                    }
                    if (mapping.BaseMapping != null)
                    {
                        if (mapping.BaseMapping.Declares(member, mapping.TypeName))
                        {
                            continue;
                        }
                    }
                    members.Add(member);
                }
                mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));
                if (mapping.BaseMapping == null)
                {
                    mapping.BaseMapping = GetRootMapping();
                }
                IncludeTypes(model.Type);
            }
            return(mapping);
        }
Exemplo n.º 34
0
 internal static void AddIncludeMetadata(CodeAttributeDeclarationCollection metadata, StructMapping mapping, Type type) {
     if (mapping.IsAnonymousType)
         return;
     for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
         CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(type.FullName);
         attribute.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(derived.TypeDesc.FullName)));
         metadata.Add(attribute);
         AddIncludeMetadata(metadata, derived, type);
     }
 }
 private void ExportDerivedMappings(StructMapping mapping)
 {
     if (!mapping.IsAnonymousType)
     {
         for (StructMapping mapping2 = mapping.DerivedMappings; mapping2 != null; mapping2 = mapping2.NextDerivedMapping)
         {
             if (mapping2.IncludeInSchema)
             {
                 this.ExportStructMapping(mapping2, mapping2.Namespace, null);
             }
         }
     }
 }
        CodeTypeDeclaration ExportStruct(StructMapping mapping)
        {
            if (mapping.TypeDesc.IsRoot)
            {
                ExportRoot(mapping, typeof(SoapIncludeAttribute));
                return(null);
            }
            if (!mapping.IncludeInSchema)
            {
                return(null);
            }

            string className = mapping.TypeDesc.Name;
            string baseName  = mapping.TypeDesc.BaseTypeDesc == null ? string.Empty : mapping.TypeDesc.BaseTypeDesc.Name;

            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);

            codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
            codeClass.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true));

            CodeNamespace.Types.Add(codeClass);

            if (baseName != null && baseName.Length > 0)
            {
                codeClass.BaseTypes.Add(baseName);
            }
            else
            {
                AddPropertyChangedNotifier(codeClass);
            }

            codeClass.TypeAttributes |= TypeAttributes.Public;
            if (mapping.TypeDesc.IsAbstract)
            {
                codeClass.TypeAttributes |= TypeAttributes.Abstract;
            }

            CodeExporter.AddIncludeMetadata(codeClass.CustomAttributes, mapping, typeof(SoapIncludeAttribute));

            if (GenerateProperties)
            {
                for (int i = 0; i < mapping.Members.Length; i++)
                {
                    ExportProperty(codeClass, mapping.Members[i], mapping.Scope);
                }
            }
            else
            {
                for (int i = 0; i < mapping.Members.Length; i++)
                {
                    ExportMember(codeClass, mapping.Members[i]);
                }
            }
            for (int i = 0; i < mapping.Members.Length; i++)
            {
                EnsureTypesExported(mapping.Members[i].Elements, null);
            }

            if (mapping.BaseMapping != null)
            {
                ExportType(mapping.BaseMapping);
            }

            ExportDerivedStructs(mapping);
            CodeGenerator.ValidateIdentifiers(codeClass);
            return(codeClass);
        }
 private XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns, XmlSchemaElement element)
 {
     if (mapping.TypeDesc.IsRoot)
     {
         this.needToExportRoot = true;
         return XmlQualifiedName.Empty;
     }
     if (mapping.IsAnonymousType)
     {
         if (this.references[mapping] != null)
         {
             throw new InvalidOperationException(Res.GetString("XmlCircularReference2", new object[] { mapping.TypeDesc.Name, "AnonymousType", "false" }));
         }
         this.references[mapping] = mapping;
     }
     XmlSchemaComplexType item = (XmlSchemaComplexType) this.types[mapping];
     if (item == null)
     {
         if (!mapping.IncludeInSchema)
         {
             throw new InvalidOperationException(Res.GetString("XmlCannotIncludeInSchema", new object[] { mapping.TypeDesc.Name }));
         }
         this.CheckForDuplicateType(mapping, mapping.Namespace);
         item = new XmlSchemaComplexType();
         if (!mapping.IsAnonymousType)
         {
             item.Name = mapping.TypeName;
             this.AddSchemaItem(item, mapping.Namespace, ns);
             this.types.Add(mapping, item);
         }
         item.IsAbstract = mapping.TypeDesc.IsAbstract;
         bool isOpenModel = mapping.IsOpenModel;
         if ((mapping.BaseMapping != null) && mapping.BaseMapping.IncludeInSchema)
         {
             if (mapping.BaseMapping.IsAnonymousType)
             {
                 throw new InvalidOperationException(Res.GetString("XmlAnonymousBaseType", new object[] { mapping.TypeDesc.Name, mapping.BaseMapping.TypeDesc.Name, "AnonymousType", "false" }));
             }
             if (mapping.HasSimpleContent)
             {
                 XmlSchemaSimpleContent content = new XmlSchemaSimpleContent();
                 XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension {
                     BaseTypeName = this.ExportStructMapping(mapping.BaseMapping, mapping.Namespace, null)
                 };
                 content.Content = extension;
                 item.ContentModel = content;
             }
             else
             {
                 XmlSchemaComplexContentExtension extension2 = new XmlSchemaComplexContentExtension {
                     BaseTypeName = this.ExportStructMapping(mapping.BaseMapping, mapping.Namespace, null)
                 };
                 XmlSchemaComplexContent content2 = new XmlSchemaComplexContent {
                     Content = extension2,
                     IsMixed = XmlSchemaImporter.IsMixed((XmlSchemaComplexType) this.types[mapping.BaseMapping])
                 };
                 item.ContentModel = content2;
             }
             isOpenModel = false;
         }
         this.ExportTypeMembers(item, mapping.Members, mapping.TypeName, mapping.Namespace, mapping.HasSimpleContent, isOpenModel);
         this.ExportDerivedMappings(mapping);
         if (mapping.XmlnsMember != null)
         {
             this.AddXmlnsAnnotation(item, mapping.XmlnsMember.Name);
         }
     }
     else
     {
         this.AddSchemaImport(mapping.Namespace, ns);
     }
     if (mapping.IsAnonymousType)
     {
         this.references[mapping] = null;
         if (element != null)
         {
             element.SchemaType = item;
         }
         return XmlQualifiedName.Empty;
     }
     XmlQualifiedName name = new XmlQualifiedName(item.Name, mapping.Namespace);
     if (element != null)
     {
         element.SchemaTypeName = name;
     }
     return name;
 }
Exemplo n.º 38
0
 internal static MemberMapping[] GetAllMembers(StructMapping mapping, System.Collections.Generic.Dictionary <string, MemberInfo> memberInfos)
 {
     MemberMapping[] mappings = GetAllMembers(mapping);
     PopulateMemberInfos(mapping, mappings, memberInfos);
     return(mappings);
 }
Exemplo n.º 39
0
        void WriteDerivedTypes(StructMapping mapping) {
            for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
                string fullTypeName = derived.TypeDesc.CSharpName;
                Writer.Write("else if (");
                WriteTypeCompare("t", fullTypeName, derived.TypeDesc.UseReflection);
                Writer.WriteLine(") {");
                Writer.Indent++;

                string methodName = ReferenceMapping(derived);

                #if DEBUG
                    // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                    if (methodName == null) throw new InvalidOperationException("deriaved from " + mapping.TypeDesc.FullName + ", " + Res.GetString(Res.XmlInternalErrorMethod, derived.TypeDesc.Name) + Environment.StackTrace);
                #endif

                Writer.Write(methodName);
                Writer.Write("(n, ns,");
                if(!derived.TypeDesc.UseReflection) Writer.Write("("+fullTypeName+")");
                Writer.Write("o");
                if (derived.TypeDesc.IsNullable)
                    Writer.Write(", isNullable");
                Writer.Write(", true");
                Writer.WriteLine(");");
                Writer.WriteLine("return;");
                Writer.Indent--;
                Writer.WriteLine("}");

                WriteDerivedTypes(derived);
            }
        }
 void WriteStructMethod(StructMapping structMapping) {
     if (structMapping.IsSoap)
         WriteEncodedStructMethod(structMapping);
     else
         WriteLiteralStructMethod(structMapping);
 }
Exemplo n.º 41
0
 private bool InitializeStructMembers(StructMapping mapping, StructModel model, RecursionLimiter limiter)
 {
     if (!mapping.IsFullyInitialized)
     {
         if (model.TypeDesc.BaseTypeDesc != null)
         {
             StructMapping mapping2 = this.ImportStructLikeMapping((StructModel)this.modelScope.GetTypeModel(model.Type.BaseType, false), limiter);
             int           index    = limiter.DeferredWorkItems.IndexOf(mapping.BaseMapping);
             if (index >= 0)
             {
                 if (!limiter.DeferredWorkItems.Contains(mapping))
                 {
                     limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                 }
                 int num2 = limiter.DeferredWorkItems.Count - 1;
                 if (index < num2)
                 {
                     ImportStructWorkItem item = limiter.DeferredWorkItems[index];
                     limiter.DeferredWorkItems[index] = limiter.DeferredWorkItems[num2];
                     limiter.DeferredWorkItems[num2]  = item;
                 }
                 return(false);
             }
             mapping.BaseMapping = mapping2;
         }
         ArrayList list = new ArrayList();
         foreach (MemberInfo info in model.GetMemberInfos())
         {
             if ((info.MemberType & (MemberTypes.Property | MemberTypes.Field)) != 0)
             {
                 SoapAttributes a = this.GetAttributes(info);
                 if (!a.SoapIgnore)
                 {
                     FieldModel fieldModel = model.GetFieldModel(info);
                     if (fieldModel != null)
                     {
                         MemberMapping member = this.ImportFieldMapping(fieldModel, a, mapping.Namespace, limiter);
                         if (member != null)
                         {
                             if ((!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum) && !member.TypeDesc.IsOptionalValue)
                             {
                                 if (model.TypeDesc.IsValueType)
                                 {
                                     throw new NotSupportedException(Res.GetString("XmlRpcRefsInValueType", new object[] { model.TypeDesc.FullName }));
                                 }
                                 if (member.TypeDesc.IsValueType)
                                 {
                                     throw new NotSupportedException(Res.GetString("XmlRpcNestedValueType", new object[] { member.TypeDesc.FullName }));
                                 }
                             }
                             if ((mapping.BaseMapping == null) || !mapping.BaseMapping.Declares(member, mapping.TypeName))
                             {
                                 list.Add(member);
                             }
                         }
                     }
                 }
             }
         }
         mapping.Members = (MemberMapping[])list.ToArray(typeof(MemberMapping));
         if (mapping.BaseMapping == null)
         {
             mapping.BaseMapping = this.GetRootMapping();
         }
         this.IncludeTypes(model.Type, limiter);
     }
     return(true);
 }
        void WriteEncodedStructMethod(StructMapping structMapping) {
            if(structMapping.TypeDesc.IsRoot)
                return;
            bool useReflection = structMapping.TypeDesc.UseReflection;
            string methodName = (string)MethodNames[structMapping];
            Writer.WriteLine();
            Writer.Write("object");
            Writer.Write(" ");
            Writer.Write(methodName);
            Writer.Write("(");
            Writer.WriteLine(") {");
            Writer.Indent++;

            Member[] members;
            bool anyFixups;
            string fixupMethodName;

            if (structMapping.TypeDesc.IsAbstract) {
                Writer.Write("throw CreateAbstractTypeException(");
                WriteQuotedCSharpString(structMapping.TypeName);
                Writer.Write(", ");
                WriteQuotedCSharpString(structMapping.Namespace);
                Writer.WriteLine(");");
                members = new Member[0];
                anyFixups = false;
                fixupMethodName = null;
            }
            else {
                WriteCreateMapping(structMapping, "o");

                MemberMapping[] mappings = TypeScope.GetSettableMembers(structMapping);
                members = new Member[mappings.Length];
                for (int i = 0; i < mappings.Length; i++) {
                    MemberMapping mapping = mappings[i];
                    CodeIdentifier.CheckValidIdentifier(mapping.Name);
                    string source = RaCodeGen.GetStringForMember("o", mapping.Name, structMapping.TypeDesc);
                    Member member = new Member(this,source, source, "a", i, mapping, GetChoiceIdentifierSource(mapping, "o", structMapping.TypeDesc));
                    if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite)
                        member.CheckSpecifiedSource = RaCodeGen.GetStringForMember("o", mapping.Name + "Specified", structMapping.TypeDesc);
                    if (!mapping.IsSequence)
                        member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
                    members[i] = member;
                }

                fixupMethodName = "fixup_" + methodName;
                anyFixups = WriteMemberFixupBegin(members, fixupMethodName, "o");
                
                // we're able to not do WriteMemberBegin here because we don't allow arrays as attributes
                
                WriteParamsRead(mappings.Length);
                WriteAttributes(members, null, "UnknownNode", "(object)o");
                Writer.WriteLine("Reader.MoveToElement();");

                Writer.WriteLine("if (Reader.IsEmptyElement) { Reader.Skip(); return o; }");
                Writer.WriteLine("Reader.ReadStartElement();");

                int loopIndex = WriteWhileNotLoopStart();
                Writer.Indent++;

                WriteMemberElements(members, "UnknownNode((object)o);", "UnknownNode((object)o);", null, null, null);
                Writer.WriteLine("Reader.MoveToContent();");

                WriteWhileLoopEnd(loopIndex);

                Writer.WriteLine("ReadEndElement();");
                Writer.WriteLine("return o;");
            }
            Writer.Indent--;
            Writer.WriteLine("}");

            if (anyFixups) WriteFixupMethod(fixupMethodName, members, structMapping.TypeDesc.CSharpName, structMapping.TypeDesc.UseReflection, true, "o");
        }
Exemplo n.º 43
0
 private StructMapping GetRootMapping()
 {
     if (_root == null)
     {
         _root = CreateRootMapping();
         _typeScope.AddTypeMapping(_root);
     }
     return _root;
 }
Exemplo n.º 44
0
 internal static void GetAllMembers(StructMapping mapping, ArrayList list)
 {
     if (mapping.BaseMapping != null)
     {
         GetAllMembers(mapping.BaseMapping, list);
     }
     for (int i = 0; i < mapping.Members.Length; i++)
     {
         list.Add(mapping.Members[i]);
     }
 }
Exemplo n.º 45
0
        private StructMapping ImportStructLikeMapping(StructModel model, RecursionLimiter limiter)
        {
            if (model.TypeDesc.Kind == TypeKind.Root) return GetRootMapping();

            SoapAttributes a = GetAttributes(model.Type);

            string typeNs = _defaultNs;
            if (a.SoapType != null && a.SoapType.Namespace != null)
                typeNs = a.SoapType.Namespace;
            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);
            typeName = XmlConvert.EncodeLocalName(typeName);

            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);
            if (mapping == null)
            {
                mapping = new StructMapping();
                mapping.IsSoap = true;
                mapping.TypeDesc = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName = typeName;
                if (a.SoapType != null) mapping.IncludeInSchema = a.SoapType.IncludeInSchema;
                _typeScope.AddTypeMapping(mapping);
                _types.Add(typeName, typeNs, mapping);
                if (limiter.IsExceededLimit)
                {
                    limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    return mapping;
                }

                limiter.Depth++;

                InitializeStructMembers(mapping, model, limiter);
                while (limiter.DeferredWorkItems.Count > 0)
                {
                    int index = limiter.DeferredWorkItems.Count - 1;
                    ImportStructWorkItem item = limiter.DeferredWorkItems[index];
                    if (InitializeStructMembers(item.Mapping, item.Model, limiter))
                    {
                        //
                        // if InitializeStructMembers returns true, then there were *no* chages to the DeferredWorkItems
                        //
#if DEBUG
                        // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                        if (index != limiter.DeferredWorkItems.Count - 1)
                            throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Count have changed"));
                        if (item != limiter.DeferredWorkItems[index])
                            throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Top have changed"));
#endif
                        // Remove the last work item
                        limiter.DeferredWorkItems.RemoveAt(index);
                    }
                }
                limiter.Depth--;
            }
            return mapping;
        }
Exemplo n.º 46
0
        private static void GetSettableMembers(StructMapping mapping, ArrayList list)
        {
            if (mapping.BaseMapping != null)
            {
                GetSettableMembers(mapping.BaseMapping, list);
            }

            if (mapping.Members != null)
            {
                foreach (MemberMapping memberMapping in mapping.Members)
                {
                    MemberInfo memberInfo = memberMapping.MemberInfo;
                    PropertyInfo propertyInfo = memberInfo as PropertyInfo;
                    if (propertyInfo != null && !CanWriteProperty(propertyInfo, memberMapping.TypeDesc))
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlReadOnlyPropertyError, propertyInfo.DeclaringType, propertyInfo.Name));
                    }
                    list.Add(memberMapping);
                }
            }
        }
Exemplo n.º 47
0
        private bool InitializeStructMembers(StructMapping mapping, StructModel model, RecursionLimiter limiter)
        {
            if (mapping.IsFullyInitialized)
                return true;
            if (model.TypeDesc.BaseTypeDesc != null)
            {
                StructMapping baseMapping = ImportStructLikeMapping((StructModel)_modelScope.GetTypeModel(model.Type.GetTypeInfo().BaseType, false), limiter);

                // check to see if the import of the baseMapping was deffered
                int baseIndex = limiter.DeferredWorkItems.IndexOf(mapping.BaseMapping);
                if (baseIndex < 0)
                {
                    mapping.BaseMapping = baseMapping;
                }
                else
                {
                    // the import of the baseMapping was deffered, make sure that the derived mappings is deffered as well
                    if (!limiter.DeferredWorkItems.Contains(mapping))
                    {
                        limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    }
                    // make sure that baseMapping get processed before the derived
                    int top = limiter.DeferredWorkItems.Count - 1;
                    if (baseIndex < top)
                    {
                        ImportStructWorkItem baseMappingWorkItem = limiter.DeferredWorkItems[baseIndex];
                        limiter.DeferredWorkItems[baseIndex] = limiter.DeferredWorkItems[top];
                        limiter.DeferredWorkItems[top] = baseMappingWorkItem;
                    }
                    return false;
                }
            }
            ArrayList members = new ArrayList();
            foreach (MemberInfo memberInfo in model.GetMemberInfos())
            {
                if (!(memberInfo is FieldInfo) && !(memberInfo is PropertyInfo))
                    continue;
                SoapAttributes memberAttrs = GetAttributes(memberInfo);
                if (memberAttrs.SoapIgnore) continue;
                FieldModel fieldModel = model.GetFieldModel(memberInfo);
                if (fieldModel == null) continue;
                MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace, limiter);
                if (member == null) continue;

                if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum && !member.TypeDesc.IsOptionalValue)
                {
                    if (model.TypeDesc.IsValueType)
                        throw new NotSupportedException(SR.Format(SR.XmlRpcRefsInValueType, model.TypeDesc.FullName));
                    if (member.TypeDesc.IsValueType)
                        throw new NotSupportedException(SR.Format(SR.XmlRpcNestedValueType, member.TypeDesc.FullName));
                }
                if (mapping.BaseMapping != null)
                {
                    if (mapping.BaseMapping.Declares(member, mapping.TypeName)) continue;
                }
                members.Add(member);
            }
            mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));
            if (mapping.BaseMapping == null) mapping.BaseMapping = GetRootMapping();
            IncludeTypes(model.Type.GetTypeInfo(), limiter);

            return true;
        }
Exemplo n.º 48
0
        private static void PopulateMemberInfos(StructMapping structMapping, MemberMapping[] memberMappings, System.Collections.Generic.Dictionary<string, MemberInfo> memberInfos)
        {
            memberInfos.Clear();
            for (int i = 0; i < memberMappings.Length; ++i)
            {
                memberInfos[memberMappings[i].Name] = memberMappings[i].MemberInfo;
                if (memberMappings[i].ChoiceIdentifier != null)
                    memberInfos[memberMappings[i].ChoiceIdentifier.MemberName] = memberMappings[i].ChoiceIdentifier.MemberInfo;
                if (memberMappings[i].CheckSpecifiedMemberInfo != null)
                    memberInfos[memberMappings[i].Name + "Specified"] = memberMappings[i].CheckSpecifiedMemberInfo;
            }

            // The scenario here is that user has one base class A and one derived class B and wants to serialize/deserialize an object of B.
            // There's one virtual property defined in A and overrided by B. Without the replacing logic below, the code generated will always
            // try to access the property defined in A, rather than B.
            // The logic here is to:
            // 1) Check current members inside memberInfos dictionary and figure out whether there's any override or new properties defined in the derived class.
            //    If so, replace the one on the base class with the one on the derived class.
            // 2) Do the same thing for the memberMapping array. Note that we need to create a new copy of MemberMapping object since the old one could still be referenced
            //    by the StructMapping of the baseclass, so updating it directly could lead to other issues.
            Dictionary<string, MemberInfo> replaceList = null;
            MemberInfo replacedInfo = null;
            foreach (KeyValuePair<string, MemberInfo> pair in memberInfos)
            {
                if (ShouldBeReplaced(pair.Value, structMapping.TypeDesc.Type, out replacedInfo))
                {
                    if (replaceList == null)
                    {
                        replaceList = new Dictionary<string, MemberInfo>();
                    }

                    replaceList.Add(pair.Key, replacedInfo);
                }
            }

            if (replaceList != null)
            {
                foreach (KeyValuePair<string, MemberInfo> pair in replaceList)
                {
                    memberInfos[pair.Key] = pair.Value;
                }
                for (int i = 0; i < memberMappings.Length; i++)
                {
                    MemberInfo mi;
                    if (replaceList.TryGetValue(memberMappings[i].Name, out mi))
                    {
                        MemberMapping newMapping = memberMappings[i].Clone();
                        newMapping.MemberInfo = mi;
                        memberMappings[i] = newMapping;
                    }
                }
            }
        }
Exemplo n.º 49
0
        private StructMapping ImportStructType(XmlSchemaComplexType type, string typeNs, bool excludeFromImport)
        {
            if (type.Name == null)
            {
                XmlSchemaElement element    = (XmlSchemaElement)type.Parent;
                XmlQualifiedName parentType = XmlSchemas.GetParentName(element);
                throw new InvalidOperationException(SR.Format(SR.XmlInvalidSchemaElementType, parentType.Name, parentType.Namespace, element.Name));
            }

            TypeDesc baseTypeDesc = null;

            Mapping baseMapping = null;

            if (!type.DerivedFrom.IsEmpty)
            {
                baseMapping = ImportType(type.DerivedFrom, excludeFromImport);

                if (baseMapping is StructMapping)
                {
                    baseTypeDesc = ((StructMapping)baseMapping).TypeDesc;
                }
                else
                {
                    baseMapping = null;
                }
            }
            if (baseMapping == null)
            {
                baseMapping = GetRootMapping();
            }
            Mapping previousMapping = (Mapping)ImportedMappings[type];

            if (previousMapping != null)
            {
                return((StructMapping)previousMapping);
            }
            string        typeName      = GenerateUniqueTypeName(Accessor.UnescapeName(type.Name));
            StructMapping structMapping = new StructMapping();

            structMapping.IsReference = Schemas.IsReference(type);
            TypeFlags flags = TypeFlags.Reference;

            if (type.IsAbstract)
            {
                flags |= TypeFlags.Abstract;
            }
            structMapping.TypeDesc    = new TypeDesc(typeName, typeName, TypeKind.Struct, baseTypeDesc, flags);
            structMapping.Namespace   = typeNs;
            structMapping.TypeName    = type.Name;
            structMapping.BaseMapping = (StructMapping)baseMapping;
            ImportedMappings.Add(type, structMapping);
            if (excludeFromImport)
            {
                structMapping.IncludeInSchema = false;
            }
            CodeIdentifiers members = new CodeIdentifiers();

            members.AddReserved(typeName);
            AddReservedIdentifiersForDataBinding(members);
            structMapping.Members = ImportTypeMembers(type, typeNs, members);
            Scope.AddTypeMapping(structMapping);
            ImportDerivedTypes(new XmlQualifiedName(type.Name, typeNs));
            return(structMapping);
        }
Exemplo n.º 50
0
 internal StructMapping GetRootMapping()
 {
     if (_root == null)
         _root = CreateRootMapping();
     return _root;
 }
        StructMapping ImportStructType(XmlSchemaComplexType type, string typeNs, bool excludeFromImport) {
            if (type.Name == null) {
                XmlSchemaElement element = (XmlSchemaElement)type.Parent;
                XmlQualifiedName parentType = XmlSchemas.GetParentName(element);
                throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSchemaElementType, parentType.Name, parentType.Namespace, element.Name));
            }

            TypeDesc baseTypeDesc = null;

            Mapping baseMapping = null;
            if (!type.DerivedFrom.IsEmpty) {
                baseMapping = ImportType(type.DerivedFrom, excludeFromImport);

                if (baseMapping is StructMapping) 
                    baseTypeDesc = ((StructMapping)baseMapping).TypeDesc;
                else
                    baseMapping = null;
            }
            if (baseMapping == null) baseMapping = GetRootMapping();
            Mapping previousMapping = (Mapping)ImportedMappings[type];
            if (previousMapping != null) {
                return (StructMapping)previousMapping;
            }
            string typeName = GenerateUniqueTypeName(Accessor.UnescapeName(type.Name));
            StructMapping structMapping = new StructMapping();
            structMapping.IsReference = Schemas.IsReference(type);
            TypeFlags flags = TypeFlags.Reference;
            if (type.IsAbstract) flags |= TypeFlags.Abstract;
            structMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Struct, baseTypeDesc, flags);
            structMapping.Namespace = typeNs;
            structMapping.TypeName = type.Name;
            structMapping.BaseMapping = (StructMapping)baseMapping;
            ImportedMappings.Add(type, structMapping);
            if (excludeFromImport)
                structMapping.IncludeInSchema = false;
            CodeIdentifiers members = new CodeIdentifiers();
            members.AddReserved(typeName);
            AddReservedIdentifiersForDataBinding(members);
            structMapping.Members = ImportTypeMembers(type, typeNs, members);
            Scope.AddTypeMapping(structMapping);
            ImportDerivedTypes(new XmlQualifiedName(type.Name, typeNs));
            return structMapping;
        }
Exemplo n.º 52
0
        internal void ExportRoot(StructMapping mapping, Type includeType) {
            if (!rootExported) {
                rootExported = true;
                ExportDerivedStructs(mapping);

                for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping) {
                    if (!derived.ReferencedByElement && derived.IncludeInSchema && !derived.IsAnonymousType) {
                        CodeAttributeDeclaration include = new CodeAttributeDeclaration(includeType.FullName);
                        include.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(derived.TypeDesc.FullName)));
                        includeMetadata.Add(include);
                    }
                }
                Hashtable typesIncluded = new Hashtable();
                foreach (TypeMapping m in Scope.TypeMappings) {
                    if (m is ArrayMapping) {
                        ArrayMapping arrayMapping = (ArrayMapping) m;
                        if (ShouldInclude(arrayMapping) && !typesIncluded.Contains(arrayMapping.TypeDesc.FullName)) {
                            CodeAttributeDeclaration include = new CodeAttributeDeclaration(includeType.FullName);
                            include.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(arrayMapping.TypeDesc.FullName)));
                            includeMetadata.Add(include);
                            typesIncluded.Add(arrayMapping.TypeDesc.FullName, string.Empty);
                            EnsureTypesExported(arrayMapping.Elements, arrayMapping.Namespace);
                        }
                    }
                }
            }
        }
 internal abstract void ExportDerivedStructs(StructMapping mapping);
 StructMapping CreateRootMapping() {
     TypeDesc typeDesc = typeScope.GetTypeDesc(typeof(object));
     StructMapping mapping = new StructMapping();
     mapping.IsSoap = true;
     mapping.TypeDesc = typeDesc;
     mapping.Members = new MemberMapping[0];
     mapping.IncludeInSchema = false;
     mapping.TypeName = Soap.UrType;
     mapping.Namespace = XmlSchema.Namespace;
     return mapping;
 }
Exemplo n.º 55
0
        private StructMapping ImportStructLikeMapping(StructModel model, RecursionLimiter limiter)
        {
            if (model.TypeDesc.Kind == TypeKind.Root)
            {
                return(GetRootMapping());
            }

            SoapAttributes a = GetAttributes(model.Type);

            string typeNs = _defaultNs;

            if (a.SoapType != null && a.SoapType.Namespace != null)
            {
                typeNs = a.SoapType.Namespace;
            }
            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);

            typeName = XmlConvert.EncodeLocalName(typeName);

            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);

            if (mapping == null)
            {
                mapping           = new StructMapping();
                mapping.IsSoap    = true;
                mapping.TypeDesc  = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName  = typeName;
                if (a.SoapType != null)
                {
                    mapping.IncludeInSchema = a.SoapType.IncludeInSchema;
                }
                _typeScope.AddTypeMapping(mapping);
                _types.Add(typeName, typeNs, mapping);
                if (limiter.IsExceededLimit)
                {
                    limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    return(mapping);
                }

                limiter.Depth++;

                InitializeStructMembers(mapping, model, limiter);
                while (limiter.DeferredWorkItems.Count > 0)
                {
                    int index = limiter.DeferredWorkItems.Count - 1;
                    ImportStructWorkItem item = limiter.DeferredWorkItems[index];
                    if (InitializeStructMembers(item.Mapping, item.Model, limiter))
                    {
                        //
                        // if InitializeStructMembers returns true, then there were *no* changes to the DeferredWorkItems
                        //
#if DEBUG
                        // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                        if (index != limiter.DeferredWorkItems.Count - 1)
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Count have changed"));
                        }
                        if (item != limiter.DeferredWorkItems[index])
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Top have changed"));
                        }
#endif
                        // Remove the last work item
                        limiter.DeferredWorkItems.RemoveAt(index);
                    }
                }
                limiter.Depth--;
            }
            return(mapping);
        }
        StructMapping ImportStructLikeMapping(StructModel model) {
            if (model.TypeDesc.Kind == TypeKind.Root) return GetRootMapping();

            SoapAttributes a = GetAttributes(model.Type);

            string typeNs = defaultNs;
            if (a.SoapType != null && a.SoapType.Namespace != null)
                typeNs = a.SoapType.Namespace;
            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);
            typeName = XmlConvert.EncodeLocalName(typeName);

            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);
            if (mapping == null) {
                mapping = new StructMapping();
                mapping.IsSoap = true;
                mapping.TypeDesc = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName = typeName;
                if (a.SoapType != null) mapping.IncludeInSchema = a.SoapType.IncludeInSchema;
                typeScope.AddTypeMapping(mapping);
                types.Add(typeName, typeNs, mapping);
                if (model.TypeDesc.BaseTypeDesc != null) {
                    mapping.BaseMapping = ImportStructLikeMapping((StructModel)modelScope.GetTypeModel(model.Type.BaseType, false));
                }
                ArrayList members = new ArrayList();
                foreach (MemberInfo memberInfo in model.GetMemberInfos()) {
                    if ((memberInfo.MemberType & (MemberTypes.Field | MemberTypes.Property)) == 0)
                        continue;
                    SoapAttributes memberAttrs = GetAttributes(memberInfo);
                    if (memberAttrs.SoapIgnore) continue;
                    FieldModel fieldModel = model.GetFieldModel(memberInfo);
                    if (fieldModel == null) continue;
                    MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace);
                    if (member == null) continue;

                    if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum && !member.TypeDesc.IsOptionalValue) {
                        if (model.TypeDesc.IsValueType)
                            throw new NotSupportedException(Res.GetString(Res.XmlRpcRefsInValueType, model.TypeDesc.FullName));
                        if (member.TypeDesc.IsValueType)
                            throw new NotSupportedException(Res.GetString(Res.XmlRpcNestedValueType, member.TypeDesc.FullName));
                    }
                    if (mapping.BaseMapping != null) {
                        if (mapping.BaseMapping.Declares(member, mapping.TypeName)) continue;
                    }
                    members.Add(member);
                }
                mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));
                if (mapping.BaseMapping == null) mapping.BaseMapping = GetRootMapping();
                IncludeTypes(model.Type);
            }
            return mapping;
        }
 internal static void AddIncludeMetadata(CodeAttributeDeclarationCollection metadata, StructMapping mapping, Type type)
 {
     if (!mapping.IsAnonymousType)
     {
         for (StructMapping mapping2 = mapping.DerivedMappings; mapping2 != null; mapping2 = mapping2.NextDerivedMapping)
         {
             CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(type.FullName);
             declaration.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(mapping2.TypeDesc.FullName)));
             metadata.Add(declaration);
             AddIncludeMetadata(metadata, mapping2, type);
         }
     }
 }
Exemplo n.º 58
0
        void WriteStructMethod(StructMapping mapping) {
            if (mapping.IsSoap && mapping.TypeDesc.IsRoot) return;
            string methodName = (string)MethodNames[mapping];

            Writer.WriteLine();
            Writer.Write("void ");
            Writer.Write(methodName);

            string fullTypeName = mapping.TypeDesc.CSharpName;

            if (mapping.IsSoap) {
                Writer.WriteLine("(object s) {");
                Writer.Indent++;
                WriteLocalDecl(fullTypeName, "o", "s", mapping.TypeDesc.UseReflection);
            }
            else {
                Writer.Write("(string n, string ns, ");
                Writer.Write(mapping.TypeDesc.UseReflection ? "object" : fullTypeName);
                Writer.Write(" o");
                if (mapping.TypeDesc.IsNullable)
                    Writer.Write(", bool isNullable");
                Writer.WriteLine(", bool needType) {");
                Writer.Indent++;
                if (mapping.TypeDesc.IsNullable) {
                    Writer.WriteLine("if ((object)o == null) {");
                    Writer.Indent++;
                    Writer.WriteLine("if (isNullable) WriteNullTagLiteral(n, ns);");
                    Writer.WriteLine("return;");
                    Writer.Indent--;
                    Writer.WriteLine("}");
                }
                Writer.WriteLine("if (!needType) {");
                Writer.Indent++;

                Writer.Write(typeof(Type).FullName);
                Writer.WriteLine(" t = o.GetType();");
                Writer.Write("if (");
                WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection);
                Writer.WriteLine(") {");
                Writer.WriteLine("}");
                WriteDerivedTypes(mapping);
                if (mapping.TypeDesc.IsRoot)
                    WriteEnumAndArrayTypes();
                Writer.WriteLine("else {");

                Writer.Indent++;
                if (mapping.TypeDesc.IsRoot) {
                    Writer.WriteLine("WriteTypedPrimitive(n, ns, o, true);");
                    Writer.WriteLine("return;");
                }
                else {
                    Writer.WriteLine("throw CreateUnknownTypeException(o);");
                }
                Writer.Indent--;
                Writer.WriteLine("}");
                Writer.Indent--;
                Writer.WriteLine("}");
            }

            if (!mapping.TypeDesc.IsAbstract) {
                if (mapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(mapping.TypeDesc.Type)) {
                    Writer.WriteLine("EscapeName = false;");
                }

                string xmlnsSource = null;
                MemberMapping[] members = TypeScope.GetAllMembers(mapping);
                int xmlnsMember = FindXmlnsIndex(members);
                if (xmlnsMember >= 0) {
                    MemberMapping member = members[xmlnsMember];
                    CodeIdentifier.CheckValidIdentifier(member.Name);
                    xmlnsSource = RaCodeGen.GetStringForMember("o", member.Name, mapping.TypeDesc);
                    if (mapping.TypeDesc.UseReflection) {
                        xmlnsSource = "(("+member.TypeDesc.CSharpName+")"+xmlnsSource+")";
                    }
                }

                if (!mapping.IsSoap) {
                    Writer.Write("WriteStartElement(n, ns, o, false, ");
                    if (xmlnsSource == null)
                        Writer.Write("null");
                    else 
                        Writer.Write(xmlnsSource);

                    Writer.WriteLine(");");
                    if (!mapping.TypeDesc.IsRoot) {
                        Writer.Write("if (needType) WriteXsiType(");
                        WriteQuotedCSharpString(mapping.TypeName);
                        Writer.Write(", ");
                        WriteQuotedCSharpString(mapping.Namespace);
                        Writer.WriteLine(");");
                    }
                }
                else if (xmlnsSource != null) {
                    WriteNamespaces(xmlnsSource);
                }
                for (int i = 0; i < members.Length; i++) {
                    MemberMapping m = members[i];
                    if (m.Attribute != null) {
                        CodeIdentifier.CheckValidIdentifier(m.Name);
                        if (m.CheckShouldPersist) {
                            Writer.Write("if (");
                            string methodInvoke = RaCodeGen.GetStringForMethodInvoke("o", fullTypeName, "ShouldSerialize"+m.Name, mapping.TypeDesc.UseReflection);
                            if (mapping.TypeDesc.UseReflection) methodInvoke = "(("+typeof(bool).FullName+")"+methodInvoke+")";
                            Writer.Write(methodInvoke);
                            Writer.WriteLine(") {");
                            Writer.Indent++;
                        }
                        if (m.CheckSpecified != SpecifiedAccessor.None) {
                            Writer.Write("if (");
                            string memberGet = RaCodeGen.GetStringForMember("o", m.Name+"Specified", mapping.TypeDesc);
                            if(mapping.TypeDesc.UseReflection) memberGet = "(("+typeof(bool).FullName+")"+ memberGet+")";
                            Writer.Write(memberGet);
                            Writer.WriteLine(") {");
                            Writer.Indent++;
                        }
                        WriteMember(RaCodeGen.GetStringForMember("o", m.Name, mapping.TypeDesc), m.Attribute, m.TypeDesc, "o");

                        if (m.CheckSpecified != SpecifiedAccessor.None) {
                            Writer.Indent--;
                            Writer.WriteLine("}");
                        }
                        if (m.CheckShouldPersist) {
                            Writer.Indent--;
                            Writer.WriteLine("}");
                        }
                    }
                }
                
                for (int i = 0; i < members.Length; i++) {
                    MemberMapping m = members[i];
                    if (m.Xmlns != null)
                        continue;
                    CodeIdentifier.CheckValidIdentifier(m.Name);
                    bool checkShouldPersist = m.CheckShouldPersist && (m.Elements.Length > 0 || m.Text != null);

                    if (checkShouldPersist) {
                        Writer.Write("if (");
                        string methodInvoke = RaCodeGen.GetStringForMethodInvoke("o", fullTypeName, "ShouldSerialize"+m.Name, mapping.TypeDesc.UseReflection);
                        if (mapping.TypeDesc.UseReflection) methodInvoke = "(("+typeof(bool).FullName+")"+methodInvoke+")";
                        Writer.Write(methodInvoke);
                        Writer.WriteLine(") {");
                        Writer.Indent++;
                    }
                    if (m.CheckSpecified != SpecifiedAccessor.None) {
                        Writer.Write("if (");
                        string memberGet = RaCodeGen.GetStringForMember("o", m.Name+"Specified", mapping.TypeDesc);
                        if(mapping.TypeDesc.UseReflection) memberGet = "(("+typeof(bool).FullName+")"+ memberGet+")";
                        Writer.Write(memberGet);
                        Writer.WriteLine(") {");
                        Writer.Indent++;
                    }

                    string choiceSource = null;
                    if (m.ChoiceIdentifier != null){
                        CodeIdentifier.CheckValidIdentifier(m.ChoiceIdentifier.MemberName);
                        choiceSource = RaCodeGen.GetStringForMember("o", m.ChoiceIdentifier.MemberName, mapping.TypeDesc);
                    }
                    WriteMember(RaCodeGen.GetStringForMember("o", m.Name, mapping.TypeDesc), choiceSource, m.ElementsSortedByDerivation, m.Text, m.ChoiceIdentifier, m.TypeDesc, true);

                    if (m.CheckSpecified != SpecifiedAccessor.None) {
                        Writer.Indent--;
                        Writer.WriteLine("}");
                    }
                    if (checkShouldPersist) {
                        Writer.Indent--;
                        Writer.WriteLine("}");
                    }
                }
                if (!mapping.IsSoap) {
                    WriteEndElement("o");
                }
            }
            Writer.Indent--;
            Writer.WriteLine("}");
        }
Exemplo n.º 59
0
        private object WriteLiteralStructMethod(StructMapping structMapping, bool isNullable, bool checkType, string defaultNamespace)
        {
            XmlQualifiedName xsiType = checkType ? GetXsiType() : null;
            bool             isNull  = false;

            if (isNullable)
            {
                isNull = ReadNull();
            }

            if (checkType)
            {
                if (structMapping.TypeDesc.IsRoot && isNull)
                {
                    if (xsiType != null)
                    {
                        return(ReadTypedNull(xsiType));
                    }

                    else
                    {
                        if (structMapping.TypeDesc.IsValueType)
                        {
                            return(ReflectionCreateObject(structMapping.TypeDesc.Type));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }

                object o = null;
                if (xsiType == null || (!structMapping.TypeDesc.IsRoot && QNameEqual(xsiType, structMapping.TypeName, structMapping.Namespace, defaultNamespace)))
                {
                    if (structMapping.TypeDesc.IsRoot)
                    {
                        return(ReadTypedPrimitive(new XmlQualifiedName(Soap.UrType, XmlSchemaConstants.Namespace)));
                    }
                }
                else if (WriteDerivedTypes(out o, structMapping, xsiType, defaultNamespace, checkType, isNullable))
                {
                    return(o);
                }
                else if (structMapping.TypeDesc.IsRoot && WriteEnumAndArrayTypes(out o, structMapping, xsiType, defaultNamespace))
                {
                    return(o);
                }
                else
                {
                    if (structMapping.TypeDesc.IsRoot)
                    {
                        return(ReadTypedPrimitive(xsiType));
                    }
                    else
                    {
                        throw CreateUnknownTypeException(xsiType);
                    }
                }
            }

            if (structMapping.TypeDesc.IsNullable && isNull)
            {
                return(null);
            }
            else if (structMapping.TypeDesc.IsAbstract)
            {
                throw CreateAbstractTypeException(structMapping.TypeName, structMapping.Namespace);
            }
            else
            {
                if (structMapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(structMapping.TypeDesc.Type))
                {
                    // #10589: To Support Serializing XmlSchemaObject
                    throw new NotImplementedException("typeof(XmlSchemaObject)");
                }

                object o = ReflectionCreateObject(structMapping.TypeDesc.Type);

                MemberMapping[] mappings         = TypeScope.GetSettableMembers(structMapping);
                MemberMapping   anyText          = null;
                MemberMapping   anyElement       = null;
                MemberMapping   anyAttribute     = null;
                Member          anyElementMember = null;
                Member          anyTextMember    = null;

                bool isSequence = structMapping.HasExplicitSequence();

                if (isSequence)
                {
                    // #10586: Currently the reflection based method treat this kind of type as normal types.
                    // But potentially we can do some optimization for types that have ordered properties.
                }

                var allMembersList       = new List <Member>(mappings.Length);
                var allMemberMappingList = new List <MemberMapping>(mappings.Length);

                for (int i = 0; i < mappings.Length; i++)
                {
                    MemberMapping mapping = mappings[i];

                    if (mapping.Text != null)
                    {
                        anyText = mapping;
                    }
                    if (mapping.Attribute != null && mapping.Attribute.Any)
                    {
                        anyAttribute = mapping;
                    }
                    if (!isSequence)
                    {
                        // find anyElement if present.
                        for (int j = 0; j < mapping.Elements.Length; j++)
                        {
                            if (mapping.Elements[j].Any && (mapping.Elements[j].Name == null || mapping.Elements[j].Name.Length == 0))
                            {
                                anyElement = mapping;
                                break;
                            }
                        }
                    }
                    else if (mapping.IsParticle && !mapping.IsSequence)
                    {
                        StructMapping declaringMapping;
                        structMapping.FindDeclaringMapping(mapping, out declaringMapping, structMapping.TypeName);
                        throw new InvalidOperationException(SR.Format(SR.XmlSequenceHierarchy, structMapping.TypeDesc.FullName, mapping.Name, declaringMapping.TypeDesc.FullName, "Order"));
                    }

                    var member = new Member(mapping);
                    if (mapping.TypeDesc.IsArrayLike)
                    {
                        if (mapping.TypeDesc.IsArrayLike && !(mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping))
                        {
                            member.Collection = new CollectionMember();
                        }
                        else if (!mapping.TypeDesc.IsArray)
                        {
                        }
                    }

                    allMemberMappingList.Add(mapping);
                    allMembersList.Add(member);

                    if (mapping == anyElement)
                    {
                        anyElementMember = member;
                    }
                    else if (mapping == anyText)
                    {
                        anyTextMember = member;
                    }
                }

                var allMembers        = allMembersList.ToArray();
                var allMemberMappings = allMemberMappingList.ToArray();

                Action <object> unknownNodeAction = (n) => UnknownNode(n);
                WriteAttributes(allMemberMappings, anyAttribute, unknownNodeAction, ref o);

                Reader.MoveToElement();
                if (Reader.IsEmptyElement)
                {
                    Reader.Skip();
                    return(o);
                }

                Reader.ReadStartElement();
                bool IsSequenceAllMembers = IsSequence(allMembers);
                if (IsSequenceAllMembers)
                {
                    // #10586: Currently the reflection based method treat this kind of type as normal types.
                    // But potentially we can do some optimization for types that have ordered properties.
                }

                UnknownNodeAction unknownNode = UnknownNodeAction.ReadUnknownNode;
                WriteMembers(ref o, allMembers, unknownNode, unknownNode, anyElementMember, anyTextMember);

                ReadEndElement();
                return(o);
            }
        }