void RegisterTypeAsKnown(Type type)
        {
            if (known_types.Contains(type))
            {
                return;
            }

            Type elementType = type;

            if (type.HasElementType)
            {
                elementType = type.GetElementType();
            }

            known_types.Add(elementType);

            /* 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];
                foreach (var t in kt.GetTypes(elementType))
                {
                    RegisterTypeAsKnown(t);
                }
            }
        }
        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);
                }
            }

            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];
                foreach (var t in kt.GetTypes(elementType))
                {
                    known_types.Add(t);
                }
            }
        }
Exemple #3
0
        private SharedContractMap RegisterContract(Type type)
        {
            QName qname = GetContractQName(type);

            if (qname == null)
            {
                return(null);
            }
            CheckStandardQName(qname);
            if (FindUserMap(qname) != null)
            {
                throw new InvalidOperationException(String.Format("There is already a registered type for XML name {0}", qname));
            }

            SharedContractMap ret = new SharedContractMap(type, qname, this);

            contracts.Add(ret);
            ret.Initialize();

            if (type.BaseType != typeof(object))
            {
                TryRegister(type.BaseType);
                if (!FindUserMap(type.BaseType).IsContractAllowedType)
                {
                    throw new InvalidDataContractException(String.Format("To be serializable by data contract, type '{0}' cannot inherit from non-contract and non-Serializable type '{1}'", type, type.BaseType));
                }
            }

            object [] attrs = type.GetCustomAttributes(typeof(KnownTypeAttribute), true);
            for (int i = 0; i < attrs.Length; i++)
            {
                KnownTypeAttribute kt = (KnownTypeAttribute)attrs [i];
                foreach (var t in kt.GetTypes(type))
                {
                    TryRegister(t);
                }
            }

            return(ret);
        }