-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ActivitySurrogateSelector.cs
162 lines (147 loc) · 6.36 KB
/
ActivitySurrogateSelector.cs
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
155
156
157
158
159
160
161
162
namespace System.Workflow.ComponentModel.Serialization
{
using System;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public sealed class ActivitySurrogateSelector : SurrogateSelector
{
private ActivitySurrogate activitySurrogate = new ActivitySurrogate();
private ActivityExecutorSurrogate activityExecutorSurrogate = new ActivityExecutorSurrogate();
private ObjectSurrogate objectSurrogate = new ObjectSurrogate();
private DependencyStoreSurrogate dependencyStoreSurrogate = new DependencyStoreSurrogate();
private XmlDocumentSurrogate domDocSurrogate = new XmlDocumentSurrogate();
private DictionarySurrogate dictionarySurrogate = new DictionarySurrogate();
private GenericQueueSurrogate genericqueueSurrogate = new GenericQueueSurrogate();
private QueueSurrogate queueSurrogate = new QueueSurrogate();
private ListSurrogate listSurrogate = new ListSurrogate();
private SimpleTypesSurrogate simpleTypesSurrogate = new SimpleTypesSurrogate();
private static ActivitySurrogateSelector defaultActivitySurrogateSelector = new ActivitySurrogateSelector();
private static Dictionary<Type, ISerializationSurrogate> surrogateCache = new Dictionary<Type, ISerializationSurrogate>();
public static ActivitySurrogateSelector Default
{
get
{
return defaultActivitySurrogateSelector;
}
}
public override ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
{
if (type == null)
throw new ArgumentNullException("type");
selector = this;
ISerializationSurrogate result = null;
bool found;
lock (surrogateCache)
{
found = surrogateCache.TryGetValue(type, out result);
}
if (found)
{
return result == null ? base.GetSurrogate(type, context, out selector) : result;
}
// if type is assignable to activity, then return the surrogate
if (typeof(Activity).IsAssignableFrom(type))
{
result = this.activitySurrogate;
}
else if (typeof(ActivityExecutor).IsAssignableFrom(type))
{
result = this.activityExecutorSurrogate;
}
else if (typeof(IDictionary<DependencyProperty, object>).IsAssignableFrom(type))
{
result = this.dependencyStoreSurrogate;
}
else if (typeof(XmlDocument).IsAssignableFrom(type))
{
result = this.domDocSurrogate;
}
/*else if (type.IsGenericType)
{
Type genericType = type.GetGenericTypeDefinition();
if (genericType == typeof(Dictionary<,>))
{
result = this.dictionarySurrogate;
}
else if (genericType == typeof(Queue<>))
{
result = this.genericqueueSurrogate;
}
else if (genericType == typeof(List<>))
{
result = this.listSurrogate;
}
}*/
else if (typeof(Queue) == type)
{
result = this.queueSurrogate;
}
else if (typeof(Guid) == type)
{
result = this.simpleTypesSurrogate;
}
//
else if (typeof(ActivityBind).IsAssignableFrom(type))
{
result = this.objectSurrogate;
}
//
else if (typeof(DependencyObject).IsAssignableFrom(type))
{
result = this.objectSurrogate;
}
lock (surrogateCache)
{
surrogateCache[type] = result;
}
return result == null ? base.GetSurrogate(type, context, out selector) : result;
}
private sealed class ObjectSurrogate : ISerializationSurrogate
{
public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
{
info.AddValue("type", obj.GetType());
string[] names = null;
MemberInfo[] members = FormatterServicesNoSerializableCheck.GetSerializableMembers(obj.GetType(), out names);
object[] memberDatas = FormatterServices.GetObjectData(obj, members);
info.AddValue("memberDatas", memberDatas);
info.SetType(typeof(ObjectSerializedRef));
}
public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
{
return null;
}
[Serializable]
private sealed class ObjectSerializedRef : IObjectReference, IDeserializationCallback
{
private Type type = null;
private object[] memberDatas = null;
[NonSerialized]
private object returnedObject = null;
Object IObjectReference.GetRealObject(StreamingContext context)
{
if (this.returnedObject == null)
this.returnedObject = FormatterServices.GetUninitializedObject(this.type);
return this.returnedObject;
}
void IDeserializationCallback.OnDeserialization(object sender)
{
if (this.returnedObject != null)
{
string[] names = null;
MemberInfo[] members = FormatterServicesNoSerializableCheck.GetSerializableMembers(this.type, out names);
FormatterServices.PopulateObjectMembers(this.returnedObject, members, this.memberDatas);
this.returnedObject = null;
}
}
}
}
}
}