Beispiel #1
0
 public ArrayLikeFixup(LogicalType collectionType, Hashtable idCache,
                       Fixup memberFixup, object memberFixupTarget,
                       string identifier)
 {
     _memberFixup       = memberFixup;
     _memberFixupTarget = memberFixupTarget;
     _identifier        = identifier;
     _IDCache           = idCache;
     m_elements         = new ArrayList();
     _collectionType    = collectionType;
     _elementType       = collectionType.TypeAccessor.NestedAccessors.Default.Type.Type;
 }
 /// <include file='doc\XmlAttributeOverrides.uex' path='docs/doc[@for="XmlAttributeOverrides.this1"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public XmlAttributes this[Type type, string member]
 {
     get
     {
         Hashtable members = (Hashtable)_types[type];
         if (members == null)
         {
             return(null);
         }
         return((XmlAttributes)members[member]);
     }
 }
 /// <include file='doc\XmlAttributeOverrides.uex' path='docs/doc[@for="XmlAttributeOverrides.Add1"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public void Add(Type type, string member, XmlAttributes attributes)
 {
     Hashtable members = (Hashtable)_types[type];
     if (members == null)
     {
         members = new Hashtable();
         _types.Add(type, members);
     }
     else if (members[member] != null)
     {
         throw new InvalidOperationException(SR.Format(SR.XmlAttributeSetAgain, type.FullName, member));
     }
     members.Add(member, attributes);
 }
        /// <include file='doc\XmlAttributeOverrides.uex' path='docs/doc[@for="XmlAttributeOverrides.Add1"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void Add(Type type, string member, XmlAttributes attributes)
        {
            Hashtable members = (Hashtable)_types[type];

            if (members == null)
            {
                members = new Hashtable();
                _types.Add(type, members);
            }
            else if (members[member] != null)
            {
                throw new InvalidOperationException(SR.Format(SR.XmlAttributeSetAgain, type.FullName, member));
            }
            members.Add(member, attributes);
        }
        internal void Add(string ns, object o, TempAssembly assembly)
        {
            TempAssemblyCacheKey key = new TempAssemblyCacheKey(ns, o);

            lock (this)
            {
                if (_cache[key] == assembly)
                {
                    return;
                }
                Hashtable clone = new Hashtable();
                foreach (object k in _cache.Keys)
                {
                    clone.Add(k, _cache[k]);
                }
                _cache      = clone;
                _cache[key] = assembly;
            }
        }
        internal static Assembly GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Evidence evidence)
        {
            Hashtable scopeTable = new Hashtable();

            foreach (XmlMapping mapping in xmlMappings)
            {
                scopeTable[mapping.Scope] = mapping;
            }
            TypeScope[] scopes = new TypeScope[scopeTable.Keys.Count];
            scopeTable.Keys.CopyTo(scopes, 0);

            string          assemblyName    = "Microsoft.GeneratedCode";
            AssemblyBuilder assemblyBuilder = CodeGenerator.CreateAssemblyBuilder(assemblyName);
            ConstructorInfo SecurityTransparentAttribute_ctor = typeof(SecurityTransparentAttribute).GetConstructor(
                CodeGenerator.InstanceBindingFlags,
                Array.Empty <Type>()
                );

            assemblyBuilder.SetCustomAttribute(new CustomAttributeBuilder(SecurityTransparentAttribute_ctor, Array.Empty <Object>()));
            CodeIdentifiers classes = new CodeIdentifiers();

            classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter");
            classes.AddUnique("XmlSerializationReader", "XmlSerializationReader");
            string suffix = null;

            if (types != null && types.Length == 1 && types[0] != null)
            {
                suffix = CodeIdentifier.MakeValid(types[0].Name);
                if (types[0].IsArray)
                {
                    suffix += "Array";
                }
            }

            ModuleBuilder moduleBuilder = CodeGenerator.CreateModuleBuilder(assemblyBuilder, assemblyName);

            string writerClass = "XmlSerializationWriter" + suffix;

            writerClass = classes.AddUnique(writerClass, writerClass);
            XmlSerializationWriterILGen writerCodeGen = new XmlSerializationWriterILGen(scopes, "public", writerClass);

            writerCodeGen.ModuleBuilder = moduleBuilder;

            writerCodeGen.GenerateBegin();
            string[] writeMethodNames = new string[xmlMappings.Length];

            for (int i = 0; i < xmlMappings.Length; i++)
            {
                writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]);
            }
            Type writerType = writerCodeGen.GenerateEnd();

            string readerClass = "XmlSerializationReader" + suffix;

            readerClass = classes.AddUnique(readerClass, readerClass);
            XmlSerializationReaderILGen readerCodeGen = new XmlSerializationReaderILGen(scopes, "public", readerClass);

            readerCodeGen.ModuleBuilder = moduleBuilder;
            readerCodeGen.CreatedTypes.Add(writerType.Name, writerType);

            readerCodeGen.GenerateBegin();
            string[] readMethodNames = new string[xmlMappings.Length];
            for (int i = 0; i < xmlMappings.Length; i++)
            {
                readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]);
            }
            readerCodeGen.GenerateEnd(readMethodNames, xmlMappings, types);

            string    baseSerializer = readerCodeGen.GenerateBaseSerializer("XmlSerializer1", readerClass, writerClass, classes);
            Hashtable serializers    = new Hashtable();

            for (int i = 0; i < xmlMappings.Length; i++)
            {
                if (serializers[xmlMappings[i].Key] == null)
                {
                    serializers[xmlMappings[i].Key] = readerCodeGen.GenerateTypedSerializer(readMethodNames[i], writeMethodNames[i], xmlMappings[i], classes, baseSerializer, readerClass, writerClass);
                }
            }
            readerCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, readerClass, readMethodNames, writerClass, writeMethodNames, serializers);


            return(writerType.GetTypeInfo().Assembly);
        }
Beispiel #7
0
 public CodeIdentifiers()
 {
     _identifiers = new Hashtable();
     _reservedIdentifiers = new Hashtable();
     _list = new ArrayList();
 }
        private bool InitializeStructMembers(StructMapping mapping, StructModel model, bool openModel, string typeName, RecursionLimiter limiter)
        {
            if (mapping.IsFullyInitialized)
                return true;

            if (model.TypeDesc.BaseTypeDesc != null)
            {
                TypeModel baseModel = _modelScope.GetTypeModel(model.Type.GetTypeInfo().BaseType, false);
                if (!(baseModel is StructModel))
                {
                    //XmlUnsupportedInheritance=Using '{0}' as a base type for a class is not supported by XmlSerializer.
                    throw new NotSupportedException(SR.Format(SR.XmlUnsupportedInheritance, model.Type.GetTypeInfo().BaseType.FullName));
                }

                StructMapping baseMapping = ImportStructLikeMapping((StructModel)baseModel, mapping.Namespace, openModel, null, limiter);
                // check to see if the import of the baseMapping was deffered
                int baseIndex = limiter.DeferredWorkItems.IndexOf(baseMapping);
                if (baseIndex < 0)
                {
                    mapping.BaseMapping = baseMapping;

                    ICollection values = mapping.BaseMapping.LocalAttributes.Values;
                    foreach (AttributeAccessor attribute in values)
                    {
                        AddUniqueAccessor(mapping.LocalAttributes, attribute);
                    }
                    if (!mapping.BaseMapping.HasExplicitSequence())
                    {
                        values = mapping.BaseMapping.LocalElements.Values;
                        foreach (ElementAccessor e in values)
                        {
                            AddUniqueAccessor(mapping.LocalElements, e);
                        }
                    }
                }
                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();
            TextAccessor textAccesor = null;
            bool hasElements = false;
            bool isSequence = false;

            foreach (MemberInfo memberInfo in model.GetMemberInfos())
            {
                if (!(memberInfo is FieldInfo || memberInfo is PropertyInfo))
                    continue;
                XmlAttributes memberAttrs = GetAttributes(memberInfo);
                if (memberAttrs.XmlIgnore) continue;
                FieldModel fieldModel = model.GetFieldModel(memberInfo);
                if (fieldModel == null) continue;
                try
                {
                    MemberMapping member = ImportFieldMapping(model, fieldModel, memberAttrs, mapping.Namespace, limiter);
                    if (member == null) continue;
                    if (mapping.BaseMapping != null)
                    {
                        if (mapping.BaseMapping.Declares(member, mapping.TypeName)) continue;
                    }
                    isSequence |= member.IsSequence;
                    // add All memeber accessors to the scope accessors
                    AddUniqueAccessor(member, mapping.LocalElements, mapping.LocalAttributes, isSequence);

                    if (member.Text != null)
                    {
                        if (!member.Text.Mapping.TypeDesc.CanBeTextValue && member.Text.Mapping.IsList)
                            throw new InvalidOperationException(SR.Format(SR.XmlIllegalTypedTextAttribute, typeName, member.Text.Name, member.Text.Mapping.TypeDesc.FullName));
                        if (textAccesor != null)
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlIllegalMultipleText, model.Type.FullName));
                        }
                        textAccesor = member.Text;
                    }
                    if (member.Xmlns != null)
                    {
                        if (mapping.XmlnsMember != null)
                            throw new InvalidOperationException(SR.Format(SR.XmlMultipleXmlns, model.Type.FullName));
                        mapping.XmlnsMember = member;
                    }
                    if (member.Elements != null && member.Elements.Length != 0)
                    {
                        hasElements = true;
                    }
                    members.Add(member);
                }
                catch (Exception e)
                {
                    throw CreateMemberReflectionException(fieldModel, e);
                }
            }
            mapping.SetContentModel(textAccesor, hasElements);
            if (isSequence)
            {
                Hashtable ids = new Hashtable();
                for (int i = 0; i < members.Count; i++)
                {
                    MemberMapping member = (MemberMapping)members[i];
                    if (!member.IsParticle)
                        continue;
                    if (member.IsSequence)
                    {
                        if (ids[member.SequenceId] != null)
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlSequenceUnique, member.SequenceId.ToString(CultureInfo.InvariantCulture), "Order", member.Name));
                        }
                        ids[member.SequenceId] = member;
                    }
                    else
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlSequenceInconsistent, "Order", member.Name));
                    }
                }
                members.Sort(new MemberMappingComparer());
            }
            mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));
            if (mapping.BaseMapping == null) mapping.BaseMapping = GetRootMapping();
            if (mapping.XmlnsMember != null && mapping.BaseMapping.HasXmlnsMember)
                throw new InvalidOperationException(SR.Format(SR.XmlMultipleXmlns, model.Type.FullName));
            IncludeTypes(model.Type.GetTypeInfo(), limiter);
            _typeScope.AddTypeMapping(mapping);

            return true;
        }
        private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context, RecursionLimiter limiter)
        {
            if (_specials == null)
                _specials = new Hashtable();
            SpecialMapping mapping = (SpecialMapping)_specials[type];
            if (mapping != null)
            {
                CheckContext(mapping.TypeDesc, context);
                return mapping;
            }
            if (typeDesc.Kind == TypeKind.Serializable)
            {
                SerializableMapping serializableMapping = null;

                // get the schema method info
                object[] attrs = type.GetTypeInfo().GetCustomAttributes(typeof(XmlSchemaProviderAttribute), false).ToArray();

                if (attrs.Length > 0)
                {
                    // new IXmlSerializable
                    XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];
                    MethodInfo method = GetMethodFromSchemaProvider(provider, type);
                    serializableMapping = new SerializableMapping(method, provider.IsAny, ns);
                    XmlQualifiedName qname = serializableMapping.XsiType;
                    if (qname != null && !qname.IsEmpty)
                    {
                        serializableMapping.TypeName = qname.Name;
                        serializableMapping.Namespace = qname.Namespace;
                    }
                    serializableMapping.TypeDesc = typeDesc;
                    serializableMapping.Type = type;
                    IncludeTypes(type.GetTypeInfo());
                }
                else
                {
                    // old IXmlSerializable
                    serializableMapping = new SerializableMapping();
                    serializableMapping.TypeDesc = typeDesc;
                    serializableMapping.Type = type;
                }
                mapping = serializableMapping;
            }
            else
            {
                mapping = new SpecialMapping();
                mapping.TypeDesc = typeDesc;
            }
            CheckContext(typeDesc, context);
            _specials.Add(type, mapping);
            _typeScope.AddTypeMapping(mapping);
            return mapping;
        }
        private void CheckAmbiguousChoice(XmlAttributes a, Type accessorType, string accessorName)
        {
            Hashtable choiceTypes = new Hashtable();

            XmlElementAttributes elements = a.XmlElements;
            if (elements != null && elements.Count >= 2 && a.XmlChoiceIdentifier == null)
            {
                for (int i = 0; i < elements.Count; i++)
                {
                    Type type = elements[i].Type == null ? accessorType : elements[i].Type;
                    if (choiceTypes.Contains(type))
                    {
                        // You need to add {0} to the '{1}'.
                        throw new InvalidOperationException(SR.Format(SR.XmlChoiceIdentiferMissing, typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
                    }
                    else
                    {
                        choiceTypes.Add(type, false);
                    }
                }
            }
            if (choiceTypes.Contains(typeof(XmlElement)) && a.XmlAnyElements.Count > 0)
            {
                // You need to add {0} to the '{1}'.
                throw new InvalidOperationException(SR.Format(SR.XmlChoiceIdentiferMissing, typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
            }

            XmlArrayItemAttributes items = a.XmlArrayItems;
            if (items != null && items.Count >= 2)
            {
                NameTable arrayTypes = new NameTable();

                for (int i = 0; i < items.Count; i++)
                {
                    Type type = items[i].Type == null ? accessorType : items[i].Type;
                    string ns = items[i].NestingLevel.ToString(CultureInfo.InvariantCulture);
                    XmlArrayItemAttribute item = (XmlArrayItemAttribute)arrayTypes[type.FullName, ns];
                    if (item != null)
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlArrayItemAmbiguousTypes, accessorName, item.ElementName, items[i].ElementName, typeof(XmlElementAttribute).Name, typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
                    }
                    else
                    {
                        arrayTypes[type.FullName, ns] = items[i];
                    }
                }
            }
        }
Beispiel #11
0
 public CodeIdentifiers()
 {
     _identifiers         = new Hashtable();
     _reservedIdentifiers = new Hashtable();
     _list = new ArrayList();
 }