示例#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
            {
                //Remove to make internal classes can be serialized
                //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);
        }
示例#2
0
        XmlSchemaSimpleType GetSchemaSimpleListType(TypeData typeData)
        {
            XmlSchemaSimpleType     stype = new XmlSchemaSimpleType();
            XmlSchemaSimpleTypeList list  = new XmlSchemaSimpleTypeList();
            TypeData itemTypeData         = TypeTranslator.GetTypeData(typeData.ListItemType);

            list.ItemTypeName = new XmlQualifiedName(itemTypeData.XmlType, XmlSchema.Namespace);
            stype.Content     = list;
            return(stype);
        }
        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);
        }
示例#5
0
        protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType)
        {
            string   value;
            TypeData td = TypeTranslator.GetTypeData(o.GetType());

            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();
        }
示例#6
0
        protected void WriteReferencedElements()
        {
            if (referencedElements == null)
            {
                return;
            }
            if (callbacks == null)
            {
                return;
            }

            while (referencedElements.Count > 0)
            {
                object            o    = referencedElements.Dequeue();
                TypeData          td   = TypeTranslator.GetTypeData(o.GetType());
                WriteCallbackInfo info = (WriteCallbackInfo)callbacks[o.GetType()];

                if (info != null)
                {
                    WriteStartElement(info.TypeName, info.TypeNs, true);
                    Writer.WriteAttributeString("id", GetId(o, false));

                    if (td.SchemaType != SchemaTypes.Array)                     // Array use its own "arrayType" attribute
                    {
                        WriteXsiType(info.TypeName, info.TypeNs);
                    }

                    info.Callback(o);
                    WriteEndElement();
                }
                else if (IsPrimitiveArray(td))
                {
                    WriteArray(o, td);
                }
            }
        }
        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));
        }
示例#9
0
        protected void WritePotentiallyReferencingElement(string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
        {
            if (o == null)
            {
                if (isNullable)
                {
                    WriteNullTagEncoded(n, ns);
                }
                return;
            }

            WriteStartElement(n, ns, true);

            CheckReferenceQueue();

            if (callbacks != null && callbacks.ContainsKey(o.GetType()))
            {
                WriteCallbackInfo info = (WriteCallbackInfo)callbacks[o.GetType()];
                if (o.GetType().IsEnum)
                {
                    info.Callback(o);
                }
                else if (suppressReference)
                {
                    Writer.WriteAttributeString("id", GetId(o, false));
                    if (ambientType != o.GetType())
                    {
                        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(o.GetType());
                if (td.SchemaType == SchemaTypes.Primitive)
                {
                    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: " + o.GetType().FullName);
                }
            }

            WriteEndElement();
        }