示例#1
0
        public XmlContract ResolveContract(Type valueType)
        {
            if (valueType == null)
            {
                throw new ArgumentNullException(nameof(valueType));
            }


            if (!contracts.TryGetValue(valueType, out XmlContract contract))
            {
                contract = fallbackResolver?.ResolveContract(valueType) ?? throw new XmlSerializationException(string.Format("Can't resolve contract for \"{0}\".", valueType));
            }

            return(contract);
        }
示例#2
0
        private XmlTypeContext CreateTypeContext(Type valueType, XmlTypeContext context)
        {
            IXmlConverter readConverter  = null;
            IXmlConverter writeConverter = null;

            foreach (var converter in converters.Concat(DefaultConverters))
            {
                if (readConverter == null && converter.CanRead(valueType))
                {
                    readConverter = converter;

                    if (writeConverter != null)
                    {
                        break;
                    }
                }

                if (writeConverter == null && converter.CanWrite(valueType))
                {
                    writeConverter = converter;

                    if (readConverter != null)
                    {
                        break;
                    }
                }
            }

            var contract = contractResolver.ResolveContract(valueType);

            readConverter  = GetConverter(contract, readConverter);
            writeConverter = GetConverter(contract, writeConverter);

            context = new XmlTypeContext(contract, readConverter, writeConverter);
            typeContextCache.TryAdd(valueType, context);
            return(context);
        }
 public static XmlContract ResolveContract <TValue>(this IXmlContractResolver contractResolver)
 {
     return(contractResolver.ResolveContract(typeof(TValue)));
 }