Example #1
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);
                }
            }

            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);
                }
            }
        }
Example #2
0
        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);
                }
            }
        }
Example #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();

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

            return(ret);
        }
Example #4
0
        static IEnumerable <Type> GetKnownTypesByReadingKnownTypeAttributes(Type typeThatHasTheKnownTypeAttributes)
        {
            foreach (Attribute attr in typeThatHasTheKnownTypeAttributes.GetCustomAttributes(typeof(KnownTypeAttribute), true))
            {
                if (attr is KnownTypeAttribute)
                {
                    KnownTypeAttribute knownTypeAttribute = (KnownTypeAttribute)attr;

                    //------------------------------------
                    // Return the type defined using the [KnownType(type)] constructor overload:
                    //------------------------------------
                    if (knownTypeAttribute.Type != null)
                    {
                        yield return(knownTypeAttribute.Type);
                    }

                    //------------------------------------
                    // Return the types returned by the method specified using the [KnownType(methodName)] constructor overload:
                    //------------------------------------
                    if (!string.IsNullOrWhiteSpace(knownTypeAttribute.MethodName))
                    {
                        string     methodName = knownTypeAttribute.MethodName;
                        MethodInfo methodInfo = typeThatHasTheKnownTypeAttributes.GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                        if (methodInfo != null)
                        {
                            var types = methodInfo.Invoke(null, null);
                            if (types is IEnumerable <Type> )
                            {
                                foreach (Type type in (IEnumerable <Type>)types)
                                {
                                    yield return(type);
                                }
                            }
                        }
                        else
                        {
                            throw new InvalidDataContractException(
                                      string.Format(
                                          "KnownTypeAttribute attribute on type '{0}' specifies a method named '{1}' to provide known types. Static method '{1}()' was not found on this type. Ensure that the method exists and is marked as static.",
                                          typeThatHasTheKnownTypeAttributes.ToString(),
                                          methodName
                                          ));
                        }
                    }
                }
            }
        }
Example #5
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);
        }