1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
8 using System.Collections.ObjectModel;
9 using System.Reflection;
11 namespace System.Runtime.Serialization
13 public static class DataContractSerializerExtensions
15 public static ISerializationSurrogateProvider GetSerializationSurrogateProvider(this DataContractSerializer serializer)
17 SurrogateProviderAdapter adapter = serializer.DataContractSurrogate as SurrogateProviderAdapter;
18 return (adapter == null) ? null : adapter.Provider;
21 public static void SetSerializationSurrogateProvider(this DataContractSerializer serializer, ISerializationSurrogateProvider provider)
23 // allocate every time, expectation is that this won't happen enough to warrant maintaining a CondtionalWeakTable.
24 IDataContractSurrogate adapter = new SurrogateProviderAdapter(provider);
26 // DCS doesn't expose a setter, access the field directly as a workaround
27 typeof(DataContractSerializer)
28 .GetField("dataContractSurrogate", BindingFlags.Instance | BindingFlags.NonPublic)
29 .SetValue(serializer, adapter);
32 private class SurrogateProviderAdapter : IDataContractSurrogate
34 private ISerializationSurrogateProvider _provider;
35 public SurrogateProviderAdapter(ISerializationSurrogateProvider provider)
40 public ISerializationSurrogateProvider Provider { get { return _provider; } }
41 public object GetCustomDataToExport(Type clrType, Type dataContractType)
43 throw NotImplemented.ByDesign;
46 public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
48 throw NotImplemented.ByDesign;
51 public Type GetDataContractType(Type type)
53 return _provider.GetSurrogateType(type);
56 public object GetDeserializedObject(object obj, Type targetType)
58 return _provider.GetDeserializedObject(obj, targetType);
61 public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
63 throw NotImplemented.ByDesign;
66 public object GetObjectToSerialize(object obj, Type targetType)
68 return _provider.GetObjectToSerialize(obj, targetType);
71 public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
73 throw NotImplemented.ByDesign;
77 public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
79 throw NotImplemented.ByDesign;