Exemplo n.º 1
0
        public static void CheckSerializableType(Type type, bool allowPrivateConstructors)
        {
            if (type.IsArray)
            {
                return;
            }

#if NET_2_0
            if (!allowPrivateConstructors && type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, empty_modifiers) == null && !type.IsAbstract && !type.IsValueType)
#else
            if (!allowPrivateConstructors && type.GetConstructor(Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
#endif
            { throw new InvalidOperationException(type.FullName + " cannot be serialized because it does not have a default public constructor"); }

            if (type.IsInterface && !TypeTranslator.GetTypeData(type).IsListType)
            {
                throw new InvalidOperationException(type.FullName + " cannot be serialized because it is an interface");
            }

            Type t    = type;
            Type oldt = null;
            do
            {
                if (!t.IsPublic && !t.IsNestedPublic)
                {
                    throw new InvalidOperationException(type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
                }
                oldt = t;
                t    = t.DeclaringType;
            }while (t != null && t != oldt);
        }
        public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type == typeof(void))
            {
                throw new InvalidOperationException("Type " + type.Name + " may not be serialized.");
            }

            return(ImportTypeMapping(TypeTranslator.GetTypeData(type),
                                     defaultNamespace));
        }
        ICollection GetReflectionMembers(Type type)
        {
            ArrayList members = new ArrayList();

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo prop in properties)
            {
                if (!prop.CanRead)
                {
                    continue;
                }
                if (!prop.CanWrite && (TypeTranslator.GetTypeData(prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray))
                {
                    continue;
                }

                SoapAttributes atts = attributeOverrides[type, prop.Name];
                if (atts == null)
                {
                    atts = new SoapAttributes(prop);
                }
                if (atts.SoapIgnore)
                {
                    continue;
                }
                XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
                members.Add(member);
            }

            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (FieldInfo field in fields)
            {
                SoapAttributes atts = attributeOverrides[type, field.Name];
                if (atts == null)
                {
                    atts = new SoapAttributes(field);
                }
                if (atts.SoapIgnore)
                {
                    continue;
                }
                XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
                members.Add(member);
            }
            return(members);
        }
        protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType)
        {
            string   value;
            TypeData td = TypeTranslator.GetTypeData(o.GetType(), null, true);

            if (td.SchemaType != SchemaTypes.Primitive)
            {
                throw new InvalidOperationException(String.Format("The type of the argument object '{0}' is not primitive.", td.FullTypeName));
            }

            if (name == null)
            {
                ns   = td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace;
                name = td.XmlType;
            }
            else
            {
                name = XmlCustomFormatter.FromXmlName(name);
            }
            Writer.WriteStartElement(name, ns);

            if (o is XmlQualifiedName)
            {
                value = FromXmlQualifiedName((XmlQualifiedName)o);
            }
            else
            {
                value = XmlCustomFormatter.ToXmlString(td, o);
            }

            if (xsiType)
            {
                if (td.SchemaType != SchemaTypes.Primitive)
                {
                    throw new InvalidOperationException(string.Format(unexpectedTypeError, o.GetType().FullName));
                }
                WriteXsiType(td.XmlType, td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace);
            }

            WriteValue(value);

            Writer.WriteEndElement();
        }
        protected void WritePotentiallyReferencingElement(string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
        {
            if (o == null)
            {
                if (isNullable)
                {
                    WriteNullTagEncoded(n, ns);
                }
                return;
            }

            var t = o.GetType();

            WriteStartElement(n, ns, true);

            CheckReferenceQueue();

            if (callbacks != null && callbacks.ContainsKey(o.GetType()))
            {
                WriteCallbackInfo info = (WriteCallbackInfo)callbacks[t];
                if (t.IsEnum)
                {
                    info.Callback(o);
                }
                else if (suppressReference)
                {
                    Writer.WriteAttributeString("id", GetId(o, false));
                    if (ambientType != t)
                    {
                        WriteXsiType(info.TypeName, info.TypeNs);
                    }
                    info.Callback(o);
                }
                else
                {
                    if (!AlreadyQueued(o))
                    {
                        referencedElements.Enqueue(o);
                    }
                    Writer.WriteAttributeString("href", "#" + GetId(o, true));
                }
            }
            else
            {
                // Must be a primitive type or array of primitives
                TypeData td = TypeTranslator.GetTypeData(t, null, true);
                if (td.SchemaType == SchemaTypes.Primitive)
                {
                    if (t != ambientType)
                    {
                        WriteXsiType(td.XmlType, XmlSchema.Namespace);
                    }
                    Writer.WriteString(XmlCustomFormatter.ToXmlString(td, o));
                }
                else if (IsPrimitiveArray(td))
                {
                    if (!AlreadyQueued(o))
                    {
                        referencedElements.Enqueue(o);
                    }
                    Writer.WriteAttributeString("href", "#" + GetId(o, true));
                }
                else
                {
                    throw new InvalidOperationException("Invalid type: " + t.FullName);
                }
            }

            WriteEndElement();
        }
        private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace)
        {
            XmlTypeMapMember mapMember;
            SoapAttributes   atts     = rmember.SoapAttributes;
            TypeData         typeData = TypeTranslator.GetTypeData(rmember.MemberType);

            if (atts.SoapAttribute != null)
            {
                // An attribute

                if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                      "Cannot serialize member '{0}' of type {1}. " +
                                                                      "SoapAttribute cannot be used to encode complex types.",
                                                                      rmember.MemberName, typeData.FullTypeName));
                }

                if (atts.SoapElement != null)
                {
                    throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
                }

                XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute();
                if (atts.SoapAttribute.AttributeName.Length == 0)
                {
                    mapAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName);
                }
                else
                {
                    mapAttribute.AttributeName = XmlConvert.EncodeLocalName(atts.SoapAttribute.AttributeName);
                }

                mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
                if (typeData.IsComplexType)
                {
                    mapAttribute.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace);
                }

                typeData  = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapAttribute.DataType);
                mapMember = mapAttribute;
                mapMember.DefaultValue = GetDefaultValue(typeData, atts.SoapDefaultValue);
            }
            else
            {
                if (typeData.SchemaType == SchemaTypes.Array)
                {
                    mapMember = new XmlTypeMapMemberList();
                }
                else
                {
                    mapMember = new XmlTypeMapMemberElement();
                }

                if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0)
                {
                    typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapElement.DataType);
                }

                // Creates an ElementInfo that identifies the element
                XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
                XmlTypeMapElementInfo     elem     = new XmlTypeMapElementInfo(mapMember, typeData);

                elem.ElementName = XmlConvert.EncodeLocalName((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName);
                elem.Namespace   = string.Empty;
                elem.IsNullable  = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
                if (typeData.IsComplexType)
                {
                    elem.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace);
                }

                infoList.Add(elem);
                ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
            }

            mapMember.TypeData      = typeData;
            mapMember.Name          = rmember.MemberName;
            mapMember.IsReturnValue = rmember.IsReturnValue;
            return(mapMember);
        }
        XmlTypeMapping ImportClassMapping(Type type, string defaultNamespace)
        {
            TypeData typeData = TypeTranslator.GetTypeData(type);

            return(ImportClassMapping(typeData, defaultNamespace));
        }