Example #1
0
        /// <summary>
        /// Maps a data contract type to an xsi:type name and namespace during serialization.
        /// </summary>
        /// <param name="dataContractType">The type to map.</param>
        /// <param name="declaredType">The type declared in the data contract.</param>
        /// <param name="knownTypeResolver">The known type resolver.</param>
        /// <param name="typeName">The xsi:type name.</param>
        /// <param name="typeNamespace">The xsi:type namespace.</param>
        /// <returns>true if mapping succeeded; otherwise, false.</returns>
        public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            if (dataContractType == typeof(ServiceResponse))
            {
                XmlDictionary dictionary = new XmlDictionary();
                typeName      = dictionary.Add("ServiceResponse");
                typeNamespace = dictionary.Add(ServiceNamespace);
                return(true);
            }

            if (dataContractType == typeof(ServiceRequest <TAuthenticationToken>))
            {
                XmlDictionary dictionary = new XmlDictionary();
                typeName      = dictionary.Add("ServiceRequest");
                typeNamespace = dictionary.Add(ServiceNamespace);
                return(true);
            }

            bool result = TokenResolver.TryResolveType(dataContractType, declaredType, knownTypeResolver, out typeName, out typeNamespace);

            if (result)
            {
                return(true);
            }

            result = EventDataResolver.TryResolveType(dataContractType, declaredType, knownTypeResolver, out typeName, out typeNamespace);
            if (result)
            {
                return(true);
            }

            result = TryResolveUnResolvedType(dataContractType, declaredType, knownTypeResolver, ref typeName, ref typeNamespace);
            if (result)
            {
                return(true);
            }

            // Defer to the known type resolver
            return(knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace));
        }
Example #2
0
        /// <summary>
        /// Maps the specified xsi:type name and namespace to a data contract type during deserialization.
        /// </summary>
        /// <returns>
        /// The type the xsi:type name and namespace is mapped to.
        /// </returns>
        /// <param name="typeName">The xsi:type name to map.</param>
        /// <param name="typeNamespace">The xsi:type namespace to map.</param>
        /// <param name="declaredType">The type declared in the data contract.</param>
        /// <param name="knownTypeResolver">The known type resolver.</param>
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            if (typeNamespace == ServiceNamespace)
            {
                if (typeName == "ServiceResponse")
                {
                    return(typeof(ServiceResponse));
                }

                if (typeName == "ServiceRequest")
                {
                    return(typeof(ServiceRequest <TAuthenticationToken>));
                }
            }

            Type result = TokenResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);

            if (result != null)
            {
                return(result);
            }

            result = EventDataResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
            if (result != null)
            {
                return(result);
            }

            result = ResolveUnResolvedName(typeName, typeNamespace, declaredType, knownTypeResolver);
            if (result != null)
            {
                return(result);
            }

            // Defer to the known type resolver
            return(knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null));
        }