/// <summary>
        /// This method determines whether this instance can manage the specified object.
        /// </summary>
        /// <param name="pType">The type to test.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns>
        public virtual SupportPriority CanManage(Type pType, IXSerializationContext pSerializationContext)
        {
            // Cache SupportPriority by type
            SupportPriority lSupportPriority;

            if (this.mSupportPriorityForType.TryGetValue(pType, out lSupportPriority) == false)
            {
                int lDepthOfInheritance = pType.DistanceTo(this.SupportedType);

                if (lDepthOfInheritance == -1)
                {
                    lSupportPriority = SupportPriority.CANNOT_SUPPORT;
                }
                else if (this.SupportedType.IsInterface)
                {
                    lSupportPriority = new SupportPriority(SupportLevel.Interface, lDepthOfInheritance);
                }
                else
                {
                    lSupportPriority = new SupportPriority(SupportLevel.Type, lDepthOfInheritance);
                }

                this.mSupportPriorityForType[pType] = lSupportPriority;
            }

            return(lSupportPriority);
        }
示例#2
0
        /// <summary>
        /// This method finds a serialization contract.
        /// </summary>
        /// <param name="pElement">The target element.</param>
        /// <returns>The best matching contract.</returns>
        private Tuple <IXSerializationContract, SupportPriority> FindContract(XElement pElement)
        {
            List <Tuple <IXSerializationContract, SupportPriority> > lAvailableContracts = new List <Tuple <IXSerializationContract, SupportPriority> >();

// ReSharper disable once LoopCanBeConvertedToQuery
            foreach (IXSerializationContract lContract in this.mContracts)
            {
                SupportPriority lPriority = lContract.CanManage(pElement, this);
                if (lPriority != null && lPriority != SupportPriority.CANNOT_SUPPORT)
                {
                    lAvailableContracts.Add(new Tuple <IXSerializationContract, SupportPriority>(lContract, lPriority));
                }
            }

            if (lAvailableContracts.Any())
            {
// ReSharper disable once PossibleNullReferenceException
                return(lAvailableContracts.OrderBy(pElt => pElt.Item2).FirstOrDefault());
            }
            return(null);
        }