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
|
using System;
using System.Reflection;
using ProtoBuf.Meta;
namespace ProtoBuf.Serializers
{
internal sealed class SurrogateSerializer : IProtoTypeSerializer, IProtoSerializer
{
public bool ReturnsValue
{
get
{
return false;
}
}
public bool RequiresOldValue
{
get
{
return true;
}
}
public Type ExpectedType
{
get
{
return this.forType;
}
}
private readonly Type forType;
private readonly Type declaredType;
private readonly MethodInfo toTail;
private readonly MethodInfo fromTail;
private IProtoTypeSerializer rootTail;
bool IProtoTypeSerializer.HasCallbacks(TypeModel.CallbackType callbackType)
{
return false;
}
bool IProtoTypeSerializer.CanCreateInstance()
{
return false;
}
object IProtoTypeSerializer.CreateInstance(ProtoReader source)
{
throw new NotSupportedException();
}
void IProtoTypeSerializer.Callback(object value, TypeModel.CallbackType callbackType, SerializationContext context)
{
}
public SurrogateSerializer(TypeModel model, Type forType, Type declaredType, IProtoTypeSerializer rootTail)
{
Helpers.DebugAssert(forType != null, "forType");
Helpers.DebugAssert(declaredType != null, "declaredType");
Helpers.DebugAssert(rootTail != null, "rootTail");
Helpers.DebugAssert(rootTail.RequiresOldValue, "RequiresOldValue");
Helpers.DebugAssert(!rootTail.ReturnsValue, "ReturnsValue");
Helpers.DebugAssert(declaredType == rootTail.ExpectedType || Helpers.IsSubclassOf(declaredType, rootTail.ExpectedType));
this.forType = forType;
this.declaredType = declaredType;
this.rootTail = rootTail;
this.toTail = this.GetConversion(model, true);
this.fromTail = this.GetConversion(model, false);
}
private static bool HasCast(TypeModel model, Type type, Type from, Type to, out MethodInfo op)
{
MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
Type type2 = null;
foreach (MethodInfo methodInfo in methods)
{
bool flag = methodInfo.ReturnType != to;
if (!flag)
{
ParameterInfo[] parameters = methodInfo.GetParameters();
bool flag2 = parameters.Length == 1 && parameters[0].ParameterType == from;
if (flag2)
{
bool flag3 = type2 == null;
if (flag3)
{
type2 = model.MapType(typeof(ProtoConverterAttribute), false);
bool flag4 = type2 == null;
if (flag4)
{
break;
}
}
bool flag5 = methodInfo.IsDefined(type2, true);
if (flag5)
{
op = methodInfo;
return true;
}
}
}
}
foreach (MethodInfo methodInfo2 in methods)
{
bool flag6 = (methodInfo2.Name != "op_Implicit" && methodInfo2.Name != "op_Explicit") || methodInfo2.ReturnType != to;
if (!flag6)
{
ParameterInfo[] parameters = methodInfo2.GetParameters();
bool flag7 = parameters.Length == 1 && parameters[0].ParameterType == from;
if (flag7)
{
op = methodInfo2;
return true;
}
}
}
op = null;
return false;
}
public MethodInfo GetConversion(TypeModel model, bool toTail)
{
Type to = toTail ? this.declaredType : this.forType;
Type from = toTail ? this.forType : this.declaredType;
MethodInfo result;
bool flag = SurrogateSerializer.HasCast(model, this.declaredType, from, to, out result) || SurrogateSerializer.HasCast(model, this.forType, from, to, out result);
if (flag)
{
return result;
}
throw new InvalidOperationException("No suitable conversion operator found for surrogate: " + this.forType.FullName + " / " + this.declaredType.FullName);
}
public void Write(object value, ProtoWriter writer)
{
ProtoDecoratorBase.s_argsWrite[0] = value;
this.rootTail.Write(this.toTail.Invoke(null, ProtoDecoratorBase.s_argsWrite), writer);
}
public object Read(object value, ProtoReader source)
{
ProtoDecoratorBase.s_argsRead[0] = value;
value = this.toTail.Invoke(null, ProtoDecoratorBase.s_argsRead);
ProtoDecoratorBase.s_argsRead[0] = this.rootTail.Read(value, source);
return this.fromTail.Invoke(null, ProtoDecoratorBase.s_argsRead);
}
}
}
|