Merge pull request #3262 from lindenlab/add_continuations_test
[mono.git] / mcs / class / Facades / System.Runtime.Serialization.Xml / DataContractSerializerExtensions.cs
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.
4
5 #if !NO_CODEDOM
6 using System.CodeDom;
7 #endif
8 using System.Collections.ObjectModel;
9 using System.Reflection;
10
11 namespace System.Runtime.Serialization
12 {
13     public static class DataContractSerializerExtensions
14     {
15         public static ISerializationSurrogateProvider GetSerializationSurrogateProvider(this DataContractSerializer serializer) 
16         {
17             SurrogateProviderAdapter adapter = serializer.DataContractSurrogate as SurrogateProviderAdapter;
18             return (adapter == null) ? null : adapter.Provider;
19         }
20
21         public static void SetSerializationSurrogateProvider(this DataContractSerializer serializer, ISerializationSurrogateProvider provider)
22         {
23             // allocate every time, expectation is that this won't happen enough to warrant maintaining a CondtionalWeakTable.
24             IDataContractSurrogate adapter = new SurrogateProviderAdapter(provider);
25
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);
30         }
31
32         private class SurrogateProviderAdapter : IDataContractSurrogate
33         {
34             private ISerializationSurrogateProvider _provider;
35             public SurrogateProviderAdapter(ISerializationSurrogateProvider provider)
36             {
37                 _provider = provider;
38             }
39
40             public ISerializationSurrogateProvider Provider { get { return _provider; } }
41             public object GetCustomDataToExport(Type clrType, Type dataContractType)
42             {
43                 throw NotImplemented.ByDesign;
44             }
45
46             public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
47             {
48                 throw NotImplemented.ByDesign;
49             }
50
51             public Type GetDataContractType(Type type)
52             {
53                 return _provider.GetSurrogateType(type);
54             }
55
56             public object GetDeserializedObject(object obj, Type targetType)
57             {
58                 return _provider.GetDeserializedObject(obj, targetType);
59             }
60
61             public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
62             {
63                 throw NotImplemented.ByDesign;
64             }
65
66             public object GetObjectToSerialize(object obj, Type targetType)
67             {
68                 return _provider.GetObjectToSerialize(obj, targetType);
69             }
70
71             public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
72             {
73                 throw NotImplemented.ByDesign;
74             }
75
76 #if !NO_CODEDOM
77             public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
78             {
79                 throw NotImplemented.ByDesign;
80             }
81 #endif
82         }
83     }
84 }