SerializerHelper.cs
5.29 KB
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
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Linq;
using Microsoft.CSharp;
using SoapCore.ServiceModel;
namespace SoapCore
{
internal class SerializerHelper
{
private readonly SoapSerializer _serializer;
public SerializerHelper(SoapSerializer serializer)
{
_serializer = serializer;
}
public object DeserializeInputParameter(System.Xml.XmlDictionaryReader xmlReader, Type parameterType, string parameterName, string parameterNs, SoapMethodParameterInfo parameterInfo = null)
{
if (xmlReader.IsStartElement(parameterName, parameterNs))
{
xmlReader.MoveToStartElement(parameterName, parameterNs);
if (xmlReader.IsStartElement(parameterName, parameterNs))
{
switch (_serializer)
{
case SoapSerializer.XmlSerializer:
if (!parameterType.IsArray || (parameterInfo != null && parameterInfo.ArrayName != null && parameterInfo.ArrayItemName == null))
{
// case [XmlElement("parameter")] int parameter
// case int[] parameter
// case [XmlArray("parameter")] int[] parameter
return DeserializeObject(xmlReader, parameterType, parameterName, parameterNs);
}
else
{
// case [XmlElement("parameter")] int[] parameter
// case [XmlArray("parameter"), XmlArrayItem(ElementName = "item")] int[] parameter
return DeserializeArray(xmlReader, parameterType, parameterName, parameterNs, parameterInfo);
}
case SoapSerializer.DataContractSerializer:
if (!parameterType.IsArray || (parameterInfo != null && parameterInfo.ArrayName != null && parameterInfo.ArrayItemName == null))
{
// case [XmlElement("parameter")] int parameter
// case int[] parameter
// case [XmlArray("parameter")] int[] parameter
return DeserializeObject(xmlReader, parameterType, parameterName, parameterNs);
}
else
{
// case [XmlElement("parameter")] int[] parameter
// case [XmlArray("parameter"), XmlArrayItem(ElementName = "item")] int[] parameter
return DeserializeArray(xmlReader, parameterType, parameterName, parameterNs, parameterInfo);
}
//return DeserializeDataContract(xmlReader, parameterType, parameterName, parameterNs);
default:
throw new NotImplementedException();
}
}
}
return null;
}
private static object DeserializeObject(System.Xml.XmlDictionaryReader xmlReader, Type parameterType, string parameterName, string parameterNs)
{
// see https://referencesource.microsoft.com/System.Xml/System/Xml/Serialization/XmlSerializer.cs.html#c97688a6c07294d5
var elementType = parameterType.GetElementType();
if (elementType == null || parameterType.IsArray)
{
elementType = parameterType;
}
var serializer = CachedXmlSerializer.GetXmlSerializer(elementType, parameterName, parameterNs);
lock (serializer)
{
if (elementType == typeof(Stream) || typeof(Stream).IsAssignableFrom(elementType))
{
xmlReader.Read();
return new MemoryStream(xmlReader.ReadContentAsBase64());
}
return serializer.Deserialize(xmlReader);
}
}
private static object DeserializeDataContract(System.Xml.XmlDictionaryReader xmlReader, Type parameterType, string parameterName, string parameterNs)
{
var elementType = parameterType.GetElementType();
if (elementType == null || parameterType.IsArray)
{
elementType = parameterType;
}
//var s = xmlReader.ReadInnerXml();
var serializer = new DataContractSerializer(elementType, parameterName, parameterNs);
return serializer.ReadObject(xmlReader, verifyObjectName: true);
}
private object DeserializeArray(System.Xml.XmlDictionaryReader xmlReader, Type parameterType, string parameterName, string parameterNs, SoapMethodParameterInfo parameterInfo)
{
//if (parameterInfo.ArrayItemName != null)
{
xmlReader.ReadStartElement(parameterName, parameterNs);
}
var elementType = parameterType.GetElementType();
var localName = parameterInfo.ArrayItemName ?? elementType.Name;
if (parameterInfo.ArrayItemName == null && elementType.Namespace.StartsWith("System"))
{
var compiler = new CSharpCodeProvider();
var type = new CodeTypeReference(elementType);
localName = compiler.GetTypeOutput(type);
}
//localName = "ComplexModelInput";
var deserializeMethod = typeof(XmlSerializerExtensions).GetGenericMethod(nameof(XmlSerializerExtensions.DeserializeArray), elementType);
var serializer = CachedXmlSerializer.GetXmlSerializer(elementType, localName, parameterNs);
object result = null;
lock (serializer)
{
result = deserializeMethod.Invoke(null, new object[] { serializer, localName, parameterNs, xmlReader });
}
//if (parameterInfo.ArrayItemName != null)
{
xmlReader.ReadEndElement();
}
return result;
}
}
}