Beispiel #1
0
        private CollectionTypeMap RegisterCollection(Type type)
        {
            Type element = GetCollectionElementType(type);

            if (element == null)
            {
                return(null);
            }

            TryRegister(element);

            QName qname = GetCollectionQName(element);

            var map = FindUserMap(qname);

            if (map != null)
            {
                var cmap = map as CollectionTypeMap;
                if (cmap == null)                 // The runtime type may still differ (between array and other IList; see bug #670560)
                {
                    throw new InvalidOperationException(String.Format("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, qname));
                }
                return(cmap);
            }

            CollectionTypeMap ret =
                new CollectionTypeMap(type, element, qname, this);

            contracts.Add(ret);
            return(ret);
        }
Beispiel #2
0
        internal void ExportListContractType(CollectionDataContractAttribute attr, CollectionTypeMap map)
        {
            Type type  = map.RuntimeType;
            var  qname = map.XmlName;

            var typeArgs = type.IsGenericType ? type.GetGenericArguments() : null;

            if (typeArgs != null && typeArgs.Length != 1)
            {
                throw new InvalidDataContractException("CollectionDataContractAttribute is applied to non-collection type.");
            }

            var  itemType = typeArgs != null ? typeArgs [0] : type.IsArray ? type.GetElementType() : typeof(object);
            bool nullable = !itemType.IsValueType;

            if (itemType.IsGenericType && itemType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                itemType = itemType.GetGenericArguments() [0];
                nullable = true;
            }
            ExportCore(itemType, false);

            var itemQName = GetSchemaTypeName(itemType);
            var itemName  = attr != null && attr.ItemName != null ? attr.ItemName : itemQName.Name;

            var ct  = CreateComplexType(qname, type);
            var seq = new XmlSchemaSequence();

            ct.Particle = seq;
            var el = new XmlSchemaElement()
            {
                Name = itemName, MinOccurs = 0, MaxOccursString = "unbounded", SchemaTypeName = itemQName, IsNillable = nullable
            };

            seq.Items.Add(el);

            /*
             * var arrayType = new XmlSchemaComplexType ();
             * el.SchemaType = arrayType;
             * var arraySeq = new XmlSchemaSequence ();
             * arrayType.Particle = arraySeq;
             * arraySeq.Items.Add (new XmlSchemaElement () { Name = itemName, SchemaTypeName = itemQName, IsNillable = true });
             */
        }
Beispiel #3
0
        private CollectionTypeMap RegisterCollection(Type type)
        {
            Type element = GetCollectionElementType(type);

            if (element == null)
            {
                return(null);
            }

            TryRegister(element);

            /*
             * To qualify as a custom collection type, a type must have
             * a public parameterless constructor and an "Add" method
             * with the correct parameter type in addition to implementing
             * one of the collection interfaces.
             *
             */

            if (!type.IsArray && type.IsClass && !IsCustomCollectionType(type, element))
            {
                return(null);
            }

            QName qname = GetCollectionQName(element);

            var map = FindUserMap(qname, element);

            if (map != null)
            {
                var cmap = map as CollectionTypeMap;
                if (cmap == null)                 // The runtime type may still differ (between array and other IList; see bug #670560)
                {
                    throw new InvalidOperationException(String.Format("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, qname));
                }
                return(cmap);
            }

            CollectionTypeMap ret =
                new CollectionTypeMap(type, element, qname, this);

            contracts.Add(ret);
            return(ret);
        }
		internal void ExportListContractType (CollectionDataContractAttribute attr, CollectionTypeMap map)
		{
			Type type = map.RuntimeType;
			var qname = map.XmlName;

			var typeArgs = type.IsGenericType ? type.GetGenericArguments () : null;
			if (typeArgs != null && typeArgs.Length != 1)
				throw new InvalidDataContractException ("CollectionDataContractAttribute is applied to non-collection type.");

			var itemType = typeArgs != null ? typeArgs [0] : type.IsArray ? type.GetElementType () : typeof (object);
			bool nullable = !itemType.IsValueType;
			if (itemType.IsGenericType && itemType.GetGenericTypeDefinition () == typeof (Nullable<>)) {
				itemType = itemType.GetGenericArguments () [0];
				nullable = true;
			}
			ExportCore (itemType, false);

			var itemQName = GetSchemaTypeName (itemType);
			var itemName = attr != null && attr.ItemName != null ? attr.ItemName : itemQName.Name;

			var ct = CreateComplexType (qname, type);
			var seq = new XmlSchemaSequence ();
			ct.Particle = seq;
			var el = new XmlSchemaElement () { Name = itemName, MinOccurs = 0, MaxOccursString = "unbounded", SchemaTypeName = itemQName, IsNillable = nullable };
			seq.Items.Add (el);

			/*
			var arrayType = new XmlSchemaComplexType ();
			el.SchemaType = arrayType;
			var arraySeq = new XmlSchemaSequence ();
			arrayType.Particle = arraySeq;
			arraySeq.Items.Add (new XmlSchemaElement () { Name = itemName, SchemaTypeName = itemQName, IsNillable = true });
			*/
		}