1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.Runtime.Serialization
{
using System;
using System.Xml;
using System.Xml.Schema;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using DataContractDictionary = System.Collections.Generic.Dictionary<System.Xml.XmlQualifiedName, DataContract>;
using System.Text;
internal class DataContractSet
{
Dictionary<XmlQualifiedName, DataContract> contracts;
Dictionary<DataContract, object> processedContracts;
IDataContractSurrogate dataContractSurrogate;
Hashtable surrogateDataTable;
DataContractDictionary knownTypesForObject;
ICollection<Type> referencedTypes;
ICollection<Type> referencedCollectionTypes;
Dictionary<XmlQualifiedName, object> referencedTypesDictionary;
Dictionary<XmlQualifiedName, object> referencedCollectionTypesDictionary;
internal DataContractSet(IDataContractSurrogate dataContractSurrogate) : this(dataContractSurrogate, null, null) { }
internal DataContractSet(IDataContractSurrogate dataContractSurrogate, ICollection<Type> referencedTypes, ICollection<Type> referencedCollectionTypes)
{
this.dataContractSurrogate = dataContractSurrogate;
this.referencedTypes = referencedTypes;
this.referencedCollectionTypes = referencedCollectionTypes;
}
internal DataContractSet(DataContractSet dataContractSet)
{
if (dataContractSet == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dataContractSet"));
this.dataContractSurrogate = dataContractSet.dataContractSurrogate;
this.referencedTypes = dataContractSet.referencedTypes;
this.referencedCollectionTypes = dataContractSet.referencedCollectionTypes;
foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
{
Add(pair.Key, pair.Value);
}
if (dataContractSet.processedContracts != null)
{
foreach (KeyValuePair<DataContract, object> pair in dataContractSet.processedContracts)
{
ProcessedContracts.Add(pair.Key, pair.Value);
}
}
}
Dictionary<XmlQualifiedName, DataContract> Contracts
{
get
{
if (contracts == null)
{
contracts = new Dictionary<XmlQualifiedName, DataContract>();
}
return contracts;
}
}
Dictionary<DataContract, object> ProcessedContracts
{
get
{
if (processedContracts == null)
{
processedContracts = new Dictionary<DataContract, object>();
}
return processedContracts;
}
}
Hashtable SurrogateDataTable
{
get
{
if (surrogateDataTable == null)
surrogateDataTable = new Hashtable();
return surrogateDataTable;
}
}
internal DataContractDictionary KnownTypesForObject
{
get { return knownTypesForObject; }
set { knownTypesForObject = value; }
}
internal void Add(Type type)
{
DataContract dataContract = GetDataContract(type);
EnsureTypeNotGeneric(dataContract.UnderlyingType);
Add(dataContract);
}
internal static void EnsureTypeNotGeneric(Type type)
{
if (type.ContainsGenericParameters)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericTypeNotExportable, type)));
}
void Add(DataContract dataContract)
{
Add(dataContract.StableName, dataContract);
}
public void Add(XmlQualifiedName name, DataContract dataContract)
{
if (dataContract.IsBuiltInDataContract)
return;
InternalAdd(name, dataContract);
}
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 System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.DupContractInDataContractSet, dataContract.StableName.Name, dataContract.StableName.Namespace)));
else
{
bool typeNamesEqual = (DataContract.GetClrTypeFullName(dataContract.UnderlyingType) == DataContract.GetClrTypeFullName(dataContractInSet.UnderlyingType));
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(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);
}
}
}
void AddClassDataContract(ClassDataContract classDataContract)
{
if (classDataContract.BaseContract != null)
{
Add(classDataContract.BaseContract.StableName, classDataContract.BaseContract);
}
if (!classDataContract.IsISerializable)
{
if (classDataContract.Members != null)
{
for (int i = 0; i < classDataContract.Members.Count; i++)
{
DataMember dataMember = classDataContract.Members[i];
DataContract memberDataContract = GetMemberTypeDataContract(dataMember);
if (dataContractSurrogate != null && dataMember.MemberInfo != null)
{
object customData = DataContractSurrogateCaller.GetCustomDataToExport(
dataContractSurrogate,
dataMember.MemberInfo,
memberDataContract.UnderlyingType);
if (customData != null)
SurrogateDataTable.Add(dataMember, customData);
}
Add(memberDataContract.StableName, memberDataContract);
}
}
}
AddKnownDataContracts(classDataContract.KnownDataContracts);
}
void AddCollectionDataContract(CollectionDataContract collectionDataContract)
{
if (collectionDataContract.IsDictionary)
{
ClassDataContract keyValueContract = collectionDataContract.ItemContract as ClassDataContract;
AddClassDataContract(keyValueContract);
}
else
{
DataContract itemContract = GetItemTypeDataContract(collectionDataContract);
if (itemContract != null)
Add(itemContract.StableName, itemContract);
}
AddKnownDataContracts(collectionDataContract.KnownDataContracts);
}
void AddXmlDataContract(XmlDataContract xmlDataContract)
{
AddKnownDataContracts(xmlDataContract.KnownDataContracts);
}
void AddKnownDataContracts(DataContractDictionary knownDataContracts)
{
if (knownDataContracts != null)
{
foreach (DataContract knownDataContract in knownDataContracts.Values)
{
Add(knownDataContract);
}
}
}
internal XmlQualifiedName GetStableName(Type clrType)
{
if (dataContractSurrogate != null)
{
Type dcType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, clrType);
//if (clrType.IsValueType != dcType.IsValueType)
// throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.ValueTypeMismatchInSurrogatedType, dcType, clrType)));
return DataContract.GetStableName(dcType);
}
return DataContract.GetStableName(clrType);
}
internal DataContract GetDataContract(Type clrType)
{
if (dataContractSurrogate == null)
return DataContract.GetDataContract(clrType);
DataContract dataContract = DataContract.GetBuiltInDataContract(clrType);
if (dataContract != null)
return dataContract;
Type dcType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, clrType);
//if (clrType.IsValueType != dcType.IsValueType)
// throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.ValueTypeMismatchInSurrogatedType, dcType, clrType)));
dataContract = DataContract.GetDataContract(dcType);
if (!SurrogateDataTable.Contains(dataContract))
{
object customData = DataContractSurrogateCaller.GetCustomDataToExport(
dataContractSurrogate, clrType, dcType);
if (customData != null)
SurrogateDataTable.Add(dataContract, customData);
}
return dataContract;
}
internal DataContract GetMemberTypeDataContract(DataMember dataMember)
{
if (dataMember.MemberInfo != null)
{
Type dataMemberType = dataMember.MemberType;
if (dataMember.IsGetOnlyCollection)
{
if (dataContractSurrogate != null)
{
Type dcType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, dataMemberType);
if (dcType != dataMemberType)
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.SurrogatesWithGetOnlyCollectionsNotSupported,
DataContract.GetClrTypeFullName(dataMemberType), DataContract.GetClrTypeFullName(dataMember.MemberInfo.DeclaringType), dataMember.MemberInfo.Name)));
}
}
return DataContract.GetGetOnlyCollectionDataContract(DataContract.GetId(dataMemberType.TypeHandle), dataMemberType.TypeHandle, dataMemberType, SerializationMode.SharedContract);
}
else
{
return GetDataContract(dataMemberType);
}
}
return dataMember.MemberTypeContract;
}
internal DataContract GetItemTypeDataContract(CollectionDataContract collectionContract)
{
if (collectionContract.ItemType != null)
return GetDataContract(collectionContract.ItemType);
return collectionContract.ItemContract;
}
internal object GetSurrogateData(object key)
{
return SurrogateDataTable[key];
}
internal void SetSurrogateData(object key, object surrogateData)
{
SurrogateDataTable[key] = surrogateData;
}
public DataContract this[XmlQualifiedName key]
{
get
{
DataContract dataContract = DataContract.GetBuiltInDataContract(key.Name, key.Namespace);
if (dataContract == null)
{
Contracts.TryGetValue(key, out dataContract);
}
return dataContract;
}
}
public IDataContractSurrogate DataContractSurrogate
{
get { return dataContractSurrogate; }
}
public bool Remove(XmlQualifiedName key)
{
if (DataContract.GetBuiltInDataContract(key.Name, key.Namespace) != null)
return false;
return Contracts.Remove(key);
}
public IEnumerator<KeyValuePair<XmlQualifiedName, DataContract>> GetEnumerator()
{
return Contracts.GetEnumerator();
}
internal bool IsContractProcessed(DataContract dataContract)
{
return ProcessedContracts.ContainsKey(dataContract);
}
internal void SetContractProcessed(DataContract dataContract)
{
ProcessedContracts.Add(dataContract, dataContract);
}
#if !NO_CODEDOM
internal ContractCodeDomInfo GetContractCodeDomInfo(DataContract dataContract)
{
object info;
if (ProcessedContracts.TryGetValue(dataContract, out info))
return (ContractCodeDomInfo)info;
return null;
}
internal void SetContractCodeDomInfo(DataContract dataContract, ContractCodeDomInfo info)
{
ProcessedContracts.Add(dataContract, info);
}
#endif
Dictionary<XmlQualifiedName, object> GetReferencedTypes()
{
if (referencedTypesDictionary == null)
{
referencedTypesDictionary = new Dictionary<XmlQualifiedName, object>();
//Always include Nullable as referenced type
//Do not allow surrogating Nullable<T>
referencedTypesDictionary.Add(DataContract.GetStableName(Globals.TypeOfNullable), Globals.TypeOfNullable);
if (this.referencedTypes != null)
{
foreach (Type type in this.referencedTypes)
{
if (type == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferencedTypesCannotContainNull)));
AddReferencedType(referencedTypesDictionary, type);
}
}
}
return referencedTypesDictionary;
}
Dictionary<XmlQualifiedName, object> GetReferencedCollectionTypes()
{
if (referencedCollectionTypesDictionary == null)
{
referencedCollectionTypesDictionary = new Dictionary<XmlQualifiedName, object>();
if (this.referencedCollectionTypes != null)
{
foreach (Type type in this.referencedCollectionTypes)
{
if (type == null)
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferencedCollectionTypesCannotContainNull)));
AddReferencedType(referencedCollectionTypesDictionary, type);
}
}
XmlQualifiedName genericDictionaryName = DataContract.GetStableName(Globals.TypeOfDictionaryGeneric);
if (!referencedCollectionTypesDictionary.ContainsKey(genericDictionaryName) && GetReferencedTypes().ContainsKey(genericDictionaryName))
AddReferencedType(referencedCollectionTypesDictionary, Globals.TypeOfDictionaryGeneric);
}
return referencedCollectionTypesDictionary;
}
void AddReferencedType(Dictionary<XmlQualifiedName, object> referencedTypes, Type type)
{
if (IsTypeReferenceable(type))
{
XmlQualifiedName stableName;
try
{
stableName = this.GetStableName(type);
}
catch (InvalidDataContractException)
{
// Type not referenceable if we can't get a stable name.
return;
}
catch (InvalidOperationException)
{
// Type not referenceable if we can't get a stable name.
return;
}
object value;
if (referencedTypes.TryGetValue(stableName, out value))
{
Type referencedType = value as Type;
if (referencedType != null)
{
if (referencedType != type)
{
referencedTypes.Remove(stableName);
List<Type> types = new List<Type>();
types.Add(referencedType);
types.Add(type);
referencedTypes.Add(stableName, types);
}
}
else
{
List<Type> types = (List<Type>)value;
if (!types.Contains(type))
types.Add(type);
}
}
else
referencedTypes.Add(stableName, type);
}
}
internal bool TryGetReferencedType(XmlQualifiedName stableName, DataContract dataContract, out Type type)
{
return TryGetReferencedType(stableName, dataContract, false/*useReferencedCollectionTypes*/, out type);
}
internal bool TryGetReferencedCollectionType(XmlQualifiedName stableName, DataContract dataContract, out Type type)
{
return TryGetReferencedType(stableName, dataContract, true/*useReferencedCollectionTypes*/, out type);
}
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(SR.GetString(((other != null && other.Equals(dataContract)) ? SR.ReferencedTypeMatchingMessage : SR.ReferencedTypeNotMatchingMessage)));
}
}
if (containsGenericType)
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
(useReferencedCollectionTypes ? SR.AmbiguousReferencedCollectionTypes1 : SR.AmbiguousReferencedTypes1),
errorMessage.ToString())));
}
else
{
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
(useReferencedCollectionTypes ? SR.AmbiguousReferencedCollectionTypes3 : SR.AmbiguousReferencedTypes3),
XmlConvert.DecodeName(stableName.Name),
stableName.Namespace,
errorMessage.ToString())));
}
}
}
type = null;
return false;
}
static bool IsTypeReferenceable(Type type)
{
Type itemType;
try
{
return (type.IsSerializable ||
type.IsDefined(Globals.TypeOfDataContractAttribute, false) ||
(Globals.TypeOfIXmlSerializable.IsAssignableFrom(type) && !type.IsGenericTypeDefinition) ||
CollectionDataContract.IsCollection(type, out itemType) ||
ClassDataContract.IsNonAttributedTypeValidForSerialization(type));
}
catch (Exception ex)
{
// An exception can be thrown in the designer when a project has a runtime binding redirection for a referenced assembly or a reference dependent assembly.
// Type.IsDefined is known to throw System.IO.FileLoadException.
// ClassDataContract.IsNonAttributedTypeValidForSerialization is known to throw System.IO.FileNotFoundException.
// We guard against all non-critical exceptions.
if (Fx.IsFatal(ex))
{
throw;
}
}
return false;
}
}
}
|