File: DataContractSerializerExtensions.cs

package info (click to toggle)
mono 5.18.0.240%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,253,216 kB
  • sloc: cs: 10,925,936; xml: 2,804,987; ansic: 643,970; cpp: 120,384; perl: 59,272; asm: 21,383; sh: 20,162; makefile: 18,157; python: 4,715; pascal: 924; sql: 859; sed: 16; php: 1
file content (84 lines) | stat: -rw-r--r-- 3,187 bytes parent folder | download | duplicates (6)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if !NO_CODEDOM
using System.CodeDom;
#endif
using System.Collections.ObjectModel;
using System.Reflection;

namespace System.Runtime.Serialization
{
    public static class DataContractSerializerExtensions
    {
        public static ISerializationSurrogateProvider GetSerializationSurrogateProvider(this DataContractSerializer serializer) 
        {
            SurrogateProviderAdapter adapter = serializer.DataContractSurrogate as SurrogateProviderAdapter;
            return (adapter == null) ? null : adapter.Provider;
        }

        public static void SetSerializationSurrogateProvider(this DataContractSerializer serializer, ISerializationSurrogateProvider provider)
        {
            // allocate every time, expectation is that this won't happen enough to warrant maintaining a CondtionalWeakTable.
            IDataContractSurrogate adapter = new SurrogateProviderAdapter(provider);

            // DCS doesn't expose a setter, access the field directly as a workaround
            typeof(DataContractSerializer)
              .GetField("dataContractSurrogate", BindingFlags.Instance | BindingFlags.NonPublic)
              .SetValue(serializer, adapter);
        }

        private class SurrogateProviderAdapter : IDataContractSurrogate
        {
            private ISerializationSurrogateProvider _provider;
            public SurrogateProviderAdapter(ISerializationSurrogateProvider provider)
            {
                _provider = provider;
            }

            public ISerializationSurrogateProvider Provider { get { return _provider; } }
            public object GetCustomDataToExport(Type clrType, Type dataContractType)
            {
                throw NotImplemented.ByDesign;
            }

            public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
            {
                throw NotImplemented.ByDesign;
            }

            public Type GetDataContractType(Type type)
            {
                return _provider.GetSurrogateType(type);
            }

            public object GetDeserializedObject(object obj, Type targetType)
            {
                return _provider.GetDeserializedObject(obj, targetType);
            }

            public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
            {
                throw NotImplemented.ByDesign;
            }

            public object GetObjectToSerialize(object obj, Type targetType)
            {
                return _provider.GetObjectToSerialize(obj, targetType);
            }

            public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
            {
                throw NotImplemented.ByDesign;
            }

#if !NO_CODEDOM
            public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
            {
                throw NotImplemented.ByDesign;
            }
#endif
        }
    }
}