bool TryGetReferencedType(XmlQualifiedName stableName, DataContract dataContract, bool useReferencedCollectionTypes, out Type type) { object value; Dictionary <XmlQualifiedName, object> referencedTypes = useReferencedCollectionTypes ? GetReferencedCollectionTypes() : GetReferencedTypes(); if (referencedTypes.TryGetValue(stableName, out value)) { type = value as Type; if (type != null) { return(true); } else { // Throw ambiguous type match exception List <Type> types = (List <Type>)value; StringBuilder errorMessage = new StringBuilder(); bool containsGenericType = false; for (int i = 0; i < types.Count; i++) { Type conflictingType = types[i]; if (!containsGenericType) { containsGenericType = conflictingType.IsGenericTypeDefinition; } errorMessage.AppendFormat("{0}\"{1}\" ", Environment.NewLine, conflictingType.AssemblyQualifiedName); if (dataContract != null) { DataContract other = this.GetDataContract(conflictingType); errorMessage.Append(((other != null && other.Equals(dataContract)) ? SR.ReferencedTypeMatchingMessage : SR.ReferencedTypeNotMatchingMessage)); } } if (containsGenericType) { throw Fx.Exception.AsError(new InvalidOperationException(useReferencedCollectionTypes ? SR.AmbiguousReferencedCollectionTypes1(errorMessage.ToString()) : SR.AmbiguousReferencedTypes1(errorMessage.ToString()))); } else { throw Fx.Exception.AsError(new InvalidOperationException(useReferencedCollectionTypes ? SR.AmbiguousReferencedCollectionTypes3(XmlConvert.DecodeName(stableName.Name), stableName.Namespace, errorMessage.ToString()) : SR.AmbiguousReferencedTypes3(XmlConvert.DecodeName(stableName.Name), stableName.Namespace, errorMessage.ToString()))); } } } type = null; return(false); }
internal void InternalAdd(XmlQualifiedName name, DataContract dataContract) { DataContract dataContractInSet = null; if (Contracts.TryGetValue(name, out dataContractInSet)) { if (!dataContractInSet.Equals(dataContract)) { if (dataContract.UnderlyingType == null || dataContractInSet.UnderlyingType == null) { throw Fx.Exception.AsError(new InvalidOperationException(SR.DupContractInDataContractSet(dataContract.StableName.Name, dataContract.StableName.Namespace))); } else { bool typeNamesEqual = (DataContract.GetClrTypeFullName(dataContract.UnderlyingType) == DataContract.GetClrTypeFullName(dataContractInSet.UnderlyingType)); throw Fx.Exception.AsError(new InvalidOperationException(SR.DupTypeContractInDataContractSet((typeNamesEqual ? dataContract.UnderlyingType.AssemblyQualifiedName : DataContract.GetClrTypeFullName(dataContract.UnderlyingType)), (typeNamesEqual ? dataContractInSet.UnderlyingType.AssemblyQualifiedName : DataContract.GetClrTypeFullName(dataContractInSet.UnderlyingType)), dataContract.StableName.Name, dataContract.StableName.Namespace))); } } } else { Contracts.Add(name, dataContract); if (dataContract is ClassDataContract) { AddClassDataContract((ClassDataContract)dataContract); } else if (dataContract is CollectionDataContract) { AddCollectionDataContract((CollectionDataContract)dataContract); } else if (dataContract is XmlDataContract) { AddXmlDataContract((XmlDataContract)dataContract); } } }