Ejemplo n.º 1
0
            public DataContractReadWriteHandler(string name, string ns, object extension, XmlObjectSerializer serializer)
            {
                if (serializer == null)
                {
                    serializer = new DataContractSerializer(extension.GetType());
                }
#if NET_4_0
                // FIXME: this is a nasty workaround that mcs somehow fails to resolve InternalVisibleTo(System.ServiceModel) in System.Runtime.Serialization.dll and thus rejects the use of KnownTypeCollection unlike NET_2_0 case (where System.ServiceModel.Web.dll is referenced).
                XmlQualifiedName qname = null;
                if (name == null || ns == null)
                {
                    var ms = new MemoryStream();
                    using (var xw = XmlWriter.Create(ms))
                        serializer.WriteStartObject(xw, extension);
                    ms.Position = 0;
                    var xr = XmlReader.Create(ms);
                    xr.MoveToContent();
                    qname = new XmlQualifiedName(xr.LocalName, xr.NamespaceURI);
                }
#else
                var qname = name == null || ns == null?KnownTypeCollection.GetStaticQName(extension.GetType()) : null;
#endif
                this.Name       = name ?? qname.Name;
                this.Namespace  = ns ?? qname.Namespace;
                this.extension  = extension;
                this.serializer = serializer;
            }
		Hashtable references = new Hashtable (); // preserve possibly referenced objects to ids. (new in 3.5 SP1)

		public static void Serialize (XmlDictionaryWriter writer, object graph,
			KnownTypeCollection types,
			bool ignoreUnknown, int maxItems, string root_ns)
		{
			new XmlFormatterSerializer (writer, types, ignoreUnknown, maxItems, root_ns)
				.Serialize (graph != null ? graph.GetType () : null, graph);
		}
		public XmlFormatterSerializer (XmlDictionaryWriter writer,
			KnownTypeCollection types,
			bool ignoreUnknown, int maxItems, string root_ns)
		{
			this.writer = writer;
			this.types = types;
			ignore_unknown = ignoreUnknown;
			max_items = maxItems;
		}
Ejemplo n.º 4
0
		public XmlFormatterSerializer (XmlDictionaryWriter writer, KnownTypeCollection types, bool ignoreUnknown,
					       int maxItems, string root_ns, bool preserveObjectReferences,
					       DataContractResolver resolver, DataContractResolver defaultResolver)
		{
			this.writer = writer;
			this.types = types;
			ignore_unknown = ignoreUnknown;
			max_items = maxItems;
			PreserveObjectReferences = preserveObjectReferences;
			this.resolver = resolver;
			this.default_resolver = defaultResolver;
		}
Ejemplo n.º 5
0
		public static object Deserialize (XmlReader reader, Type declaredType,
			KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, DataContractResolver resolver, DataContractResolver defaultResolver,
			string name, string ns, bool verifyObjectName)
		{
			reader.MoveToContent ();
			if (verifyObjectName)
				if (reader.NodeType != XmlNodeType.Element ||
				    reader.LocalName != name ||
				    reader.NamespaceURI != ns)
					throw new SerializationException (String.Format ("Expected element '{0}' in namespace '{1}', but found {2} node '{3}' in namespace '{4}'", name, ns, reader.NodeType, reader.LocalName, reader.NamespaceURI));
//				Verify (knownTypes, declaredType, name, ns, reader);
			return new XmlFormatterDeserializer (knownTypes, surrogate, resolver, defaultResolver).Deserialize (declaredType, reader);
		}
Ejemplo n.º 6
0
		public DataContractSerializer (Type type,
			IEnumerable<Type> knownTypes)
		{
			if (type == null)
				throw new ArgumentNullException ("type");
			this.type = type;
			known_types = new KnownTypeCollection ();
			PopulateTypes (knownTypes);
			known_types.TryRegister (type);
			QName qname = known_types.GetQName (type);

			FillDictionaryString (qname.Name, qname.Namespace);
			
		}
Ejemplo n.º 7
0
		// Verify the top element name and namespace.
		private static void Verify (KnownTypeCollection knownTypes, Type type, string name, string Namespace, XmlReader reader)
		{
			QName graph_qname = new QName (reader.LocalName, reader.NamespaceURI);
			if (graph_qname.Name == name && graph_qname.Namespace == Namespace)
				return;

			// <BClass .. i:type="EClass" >..</BClass>
			// Expecting type EClass : allowed
			// See test Serialize1b, and Serialize1c (for
			// negative cases)

			// Run through inheritance heirarchy .. 
			for (Type baseType = type; baseType != null; baseType = baseType.BaseType)
				if (knownTypes.GetQName (baseType) == graph_qname)
					return;

			QName typeQName = knownTypes.GetQName (type);
			throw new SerializationException (String.Format (
				"Expecting element '{0}' from namespace '{1}'. Encountered 'Element' with name '{2}', namespace '{3}'",
				typeQName.Name, typeQName.Namespace, graph_qname.Name, graph_qname.Namespace));
		}
Ejemplo n.º 8
0
		void PopulateTypes (IEnumerable<Type> knownTypes)
		{
			if (known_types == null)
				known_types= new KnownTypeCollection ();

			if (knownTypes != null) {
				foreach (Type t in knownTypes)
					known_types.Add (t);
			}

			RegisterTypeAsKnown (type);
		}
Ejemplo n.º 9
0
		Dictionary<object,string> references = new Dictionary<object,string> (); // preserve possibly referenced objects to ids. (new in 3.5 SP1)

		public static void Serialize (XmlDictionaryWriter writer, object graph, Type declaredType, KnownTypeCollection types,
			bool ignoreUnknown, int maxItems, string root_ns, bool preserveObjectReferences, DataContractResolver resolver, DataContractResolver defaultResolver)
		{
			new XmlFormatterSerializer (writer, types, ignoreUnknown, maxItems, root_ns, preserveObjectReferences, resolver, defaultResolver)
				.Serialize (/*graph != null ? graph.GetType () : */declaredType, graph); // FIXME: I believe it should always use declaredType, but such a change brings some test breakages.
		}
Ejemplo n.º 10
0
		private XmlFormatterDeserializer (
			KnownTypeCollection knownTypes,
			IDataContractSurrogate surrogate,
			DataContractResolver resolver,
			DataContractResolver defaultResolver)
		{
			this.types = knownTypes;
			this.surrogate = surrogate;
			this.resolver = resolver;
			this.default_resolver = defaultResolver;
		}
Ejemplo n.º 11
0
		Type GetTypeFromNamePair (string name, string ns)
		{
			Type p = KnownTypeCollection.GetPrimitiveTypeFromName (new QName (name, ns));
			if (p != null)
				return p;
			bool makeArray = false;
			if (name.StartsWith ("ArrayOf", StringComparison.Ordinal)) {
				name = name.Substring (7); // strip "ArrayOf"
				if (ns == KnownTypeCollection.MSArraysNamespace)
					return GetTypeFromNamePair (name, String.Empty).MakeArrayType ();
				makeArray = true;
			}

			string dnsb = KnownTypeCollection.DefaultClrNamespaceBase;
			string clrns = ns.StartsWith (dnsb, StringComparison.Ordinal) ?  ns.Substring (dnsb.Length) : ns;

			foreach (var ass in AppDomain.CurrentDomain.GetAssemblies ()) {
				Type [] types;

#if MOONLIGHT
				try  {
					types = ass.GetTypes ();
				} catch (System.Reflection.ReflectionTypeLoadException rtle) {
					types = rtle.Types;
				}
#else
				types = ass.GetTypes ();
#endif
				if (types == null)
					continue;

				foreach (var t in types) {
					// there can be null entries or exception throw to access the attribute - 
					// at least when some referenced assemblies could not be loaded (affects moonlight)
					if (t == null)
						continue;

					try {
						var dca = t.GetCustomAttribute<DataContractAttribute> (true);
						if (dca != null && dca.Name == name && dca.Namespace == ns)
							return makeArray ? t.MakeArrayType () : t;
					}
					catch (TypeLoadException tle) {
						Console.Error.WriteLine (tle);
						continue;
					}
					catch (FileNotFoundException fnfe) {
						Console.Error.WriteLine (fnfe);
						continue;
					}

					if (clrns != null && t.Name == name && t.Namespace == clrns)
						return makeArray ? t.MakeArrayType () : t;
				}
			}
			throw new XmlException (String.Format ("Type not found; name: {0}, namespace: {1}", name, ns));
		}
Ejemplo n.º 12
0
		private XmlFormatterDeserializer (
			KnownTypeCollection knownTypes,
			IDataContractSurrogate surrogate)
		{
			this.types = knownTypes;
			this.surrogate = surrogate;
		}
Ejemplo n.º 13
0
		void PopulateTypes (IEnumerable<Type> knownTypes)
		{
			if (known_types == null)
				known_types= new KnownTypeCollection ();

			if (knownTypes != null) {
				foreach (Type t in knownTypes)
					known_types.TryRegister (t);
			}

			Type elementType = type;
			if (type.HasElementType)
				elementType = type.GetElementType ();

			/* Get all KnownTypeAttribute-s, including inherited ones */
			object [] attrs = elementType.GetCustomAttributes (typeof (KnownTypeAttribute), true);
			for (int i = 0; i < attrs.Length; i ++) {
				KnownTypeAttribute kt = (KnownTypeAttribute) attrs [i];
				known_types.TryRegister (kt.Type);
			}
		}