Пример #1
0
        /// <summary>
        /// Adds support for an additional type in this model, optionally
        /// appplying inbuilt patterns. If the type is already known to the
        /// model, the existing type is returned **without** applying
        /// any additional behaviour.
        /// </summary>
        /// <remarks>Inbuilt patterns include:
        /// [ProtoContract]/[ProtoMember(n)]
        /// [DataContract]/[DataMember(Order=n)]
        /// [XmlType]/[XmlElement(Order=n)]
        /// [On{Des|S}erializ{ing|ed}]
        /// ShouldSerialize*/*Specified
        /// </remarks>
        /// <param name="type">The type to be supported</param>
        /// <param name="applyDefaultBehaviour">Whether to apply the inbuilt configuration patterns (via attributes etc), or
        /// just add the type with no additional configuration (the type must then be manually configured).</param>
        /// <returns>The MetaType representing this type, allowing
        /// further configuration.</returns>
        public MetaType Add(Type type, bool applyDefaultBehaviour)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            MetaType newType = FindWithoutAdd(type);

            if (newType != null)
            {
                return(newType);                 // return existing
            }
            bool lockTaken = false;

            if (type.IsInterface && typeof(IEnumerable).IsAssignableFrom(type) &&
                GetListItemType(type) == null)
            {
                throw new ArgumentException("IEnumerable[<T>] data cannot be used as a meta-type unless an Add method can be resolved");
            }
            try
            {
                newType = RecogniseCommonTypes(type);
                if (newType != null)
                {
                    if (!applyDefaultBehaviour)
                    {
                        throw new ArgumentException(
                                  "Default behaviour must be observed for certain types with special handling; " + type.Name,
                                  "applyDefaultBehaviour");
                    }
                    // we should assume that type is fully configured, though; no need to re-run:
                    applyDefaultBehaviour = false;
                }
                if (newType == null)
                {
                    newType = Create(type);
                }
                newType.Pending = true;
                TakeLock(ref lockTaken);
                // double checked
                if (FindWithoutAdd(type) != null)
                {
                    throw new ArgumentException("Duplicate type", "type");
                }
                ThrowIfFrozen();
                types.Add(newType);
                if (applyDefaultBehaviour)
                {
                    newType.ApplyDefaultBehaviour();
                }
                newType.Pending = false;
            }
            finally
            {
                ReleaseLock(lockTaken);
            }

            return(newType);
        }
        // Token: 0x06000375 RID: 885 RVA: 0x00012FA4 File Offset: 0x000111A4
        public MetaType Add(Type type, bool applyDefaultBehaviour)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            MetaType metaType = this.FindWithoutAdd(type);

            if (metaType != null)
            {
                return(metaType);
            }
            int opaqueToken = 0;

            if (type.IsInterface && base.MapType(MetaType.ienumerable).IsAssignableFrom(type) && TypeModel.GetListItemType(this, type) == null)
            {
                throw new ArgumentException("IEnumerable[<T>] data cannot be used as a meta-type unless an Add method can be resolved");
            }
            try
            {
                metaType = this.RecogniseCommonTypes(type);
                if (metaType != null)
                {
                    if (!applyDefaultBehaviour)
                    {
                        throw new ArgumentException("Default behaviour must be observed for certain types with special handling; " + type.FullName, "applyDefaultBehaviour");
                    }
                    applyDefaultBehaviour = false;
                }
                if (metaType == null)
                {
                    metaType = this.Create(type);
                }
                metaType.Pending = true;
                this.TakeLock(ref opaqueToken);
                if (this.FindWithoutAdd(type) != null)
                {
                    throw new ArgumentException("Duplicate type", "type");
                }
                this.ThrowIfFrozen();
                this.types.Add(metaType);
                if (applyDefaultBehaviour)
                {
                    metaType.ApplyDefaultBehaviour();
                }
                metaType.Pending = false;
            }
            finally
            {
                this.ReleaseLock(opaqueToken);
            }
            return(metaType);
        }
Пример #3
0
        /// <summary>
        /// Adds support for an additional type in this model, optionally
        /// appplying inbuilt patterns.
        /// </summary>
        /// <remarks>Inbuild patterns include:
        /// [ProtoContract]/[ProtoMember(n)]
        /// [DataContract]/[DataMember(Order=n)]
        /// [XmlType]/[XmlElement(Order=n)]
        /// [On{Des|S}erializ{ing|ed}]
        /// ShouldSerialize*/*Specified
        /// </remarks>
        /// <param name="type">The type to be supported</param>
        /// <param name="applyDefaultBehaviour">Whether to apply the inbuilt patterns, or
        /// jut add the type with no additional configuration.</param>
        /// <returns>The MetaType representing this type, allowing
        /// further configuration.</returns>
        public MetaType Add(Type type, bool applyDefaultBehaviour)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (FindWithoutAdd(type) != null)
            {
                throw new ArgumentException("Duplicate type", "type");
            }
            MetaType newType = RecogniseCommonTypes(type);

            if (newType != null)
            {
                if (!applyDefaultBehaviour)
                {
                    throw new ArgumentException(
                              "Default behaviour must be observed for certain types with special handling; " + type.Name,
                              "applyDefaultBehaviour");
                }
                // we should assume that type is fully configured, though; no need to re-run:
                applyDefaultBehaviour = false;
            }
            if (newType == null)
            {
                newType = Create(type);
            }
            bool weAdded = false;

            lock (types)
            {
                // double checked
                if (FindWithoutAdd(type) != null)
                {
                    throw new ArgumentException("Duplicate type", "type");
                }
                ThrowIfFrozen();
                types.Add(newType);
                weAdded = true;
            }
            if (weAdded && applyDefaultBehaviour)
            {
                newType.ApplyDefaultBehaviour();
            }
            return(newType);
        }