Ejemplo n.º 1
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 });
             */
        }
Ejemplo n.º 2
0
        public DictionaryTypeMap(
            Type type, CollectionDataContractAttribute a, KnownTypeCollection knownTypes)
            : base(type, QName.Empty, knownTypes)
        {
            this.a = a;

            key_type   = typeof(object);
            value_type = typeof(object);

            var idic = GetGenericDictionaryInterface(RuntimeType);

            if (idic != null)
            {
                var imap = RuntimeType.GetInterfaceMap(idic);
                for (int i = 0; i < imap.InterfaceMethods.Length; i++)
                {
                    if (imap.InterfaceMethods [i].Name == "Add")
                    {
                        add_method = imap.TargetMethods [i];
                        break;
                    }
                }
                var argtypes = idic.GetGenericArguments();
                key_type   = argtypes [0];
                value_type = argtypes [1];
                if (add_method == null)
                {
                    add_method = type.GetMethod("Add", argtypes);
                }
            }

            XmlName     = GetDictionaryQName();
            item_qname  = GetItemQName();
            key_qname   = GetKeyQName();
            value_qname = GetValueQName();
        }
 private static XmlQualifiedName GetCollectionStableName(Type type, Type itemType, HashSet<Type> previousCollectionTypes, out CollectionDataContractAttribute collectionContractAttribute)
 {
     string defaultStableLocalName;
     string defaultDataContractNamespace;
     object[] customAttributes = type.GetCustomAttributes(Globals.TypeOfCollectionDataContractAttribute, false);
     if ((customAttributes != null) && (customAttributes.Length > 0))
     {
         collectionContractAttribute = (CollectionDataContractAttribute) customAttributes[0];
         if (collectionContractAttribute.IsNameSetExplicit)
         {
             defaultStableLocalName = collectionContractAttribute.Name;
             if ((defaultStableLocalName == null) || (defaultStableLocalName.Length == 0))
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("InvalidCollectionContractName", new object[] { GetClrTypeFullName(type) })));
             }
             if (type.IsGenericType && !type.IsGenericTypeDefinition)
             {
                 defaultStableLocalName = ExpandGenericParameters(defaultStableLocalName, type);
             }
             defaultStableLocalName = EncodeLocalName(defaultStableLocalName);
         }
         else
         {
             defaultStableLocalName = GetDefaultStableLocalName(type);
         }
         if (collectionContractAttribute.IsNamespaceSetExplicit)
         {
             defaultDataContractNamespace = collectionContractAttribute.Namespace;
             if (defaultDataContractNamespace == null)
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("InvalidCollectionContractNamespace", new object[] { GetClrTypeFullName(type) })));
             }
             CheckExplicitDataContractNamespaceUri(defaultDataContractNamespace, type);
         }
         else
         {
             defaultDataContractNamespace = GetDefaultDataContractNamespace(type);
         }
     }
     else
     {
         bool flag;
         collectionContractAttribute = null;
         string str3 = "ArrayOf" + GetArrayPrefix(ref itemType);
         XmlQualifiedName name = GetStableName(itemType, previousCollectionTypes, out flag);
         defaultStableLocalName = str3 + name.Name;
         defaultDataContractNamespace = GetCollectionNamespace(name.Namespace);
     }
     return CreateQualifiedName(defaultStableLocalName, defaultDataContractNamespace);
 }
 internal static XmlQualifiedName GetCollectionStableName(Type type, Type itemType, out CollectionDataContractAttribute collectionContractAttribute)
 {
     return GetCollectionStableName(type, itemType, new HashSet<Type>(), out collectionContractAttribute);
 }
Ejemplo n.º 5
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 });
			*/
		}
Ejemplo n.º 6
0
		internal void ExportDictionaryContractType (CollectionDataContractAttribute attr, SerializationMap map, Type dicType)
		{
			var type = map.RuntimeType;
			var qname = map.XmlName;

			var typeArgs = dicType.IsGenericType ? dicType.GetGenericArguments () : null;
			var keyType = typeArgs != null ? typeArgs [0] : typeof (object);
			var valueType = typeArgs != null ? typeArgs [1] : typeof (object);
			ExportCore (keyType, false);
			ExportCore (valueType, false);

			string keyName = "Key", valueName = "Value";
			if (attr != null) {
				keyName = attr.KeyName ?? keyName;
				valueName = attr.ValueName ?? valueName;
			}
			string itemName = attr != null && attr.ItemName != null ? attr.ItemName : "KeyValueOf" + keyName + valueName;

			var ct = CreateComplexType (qname, type);
			var appInfo = new XmlSchemaAppInfo ();
			var node = new XmlDocument ().CreateElement ("IsDictionary", KnownTypeCollection.MSSimpleNamespace);
			node.InnerText = "true";
			appInfo.Markup = new XmlNode [] { node };
			ct.Annotation = new XmlSchemaAnnotation ();
			ct.Annotation.Items.Add (appInfo);

			var seq = new XmlSchemaSequence ();
			ct.Particle = seq;
			var el = new XmlSchemaElement () { Name = itemName, MinOccurs = 0, MaxOccursString = "unbounded" };
			seq.Items.Add (el);

			var dictType = new XmlSchemaComplexType ();
			el.SchemaType = dictType;
			var dictSeq = new XmlSchemaSequence ();
			dictType.Particle = dictSeq;
			dictSeq.Items.Add (new XmlSchemaElement () { Name = keyName, SchemaTypeName = GetSchemaTypeName (keyType), IsNillable = true });
			dictSeq.Items.Add (new XmlSchemaElement () { Name = valueName, SchemaTypeName = GetSchemaTypeName (valueType), IsNillable = true });
		}