/
EnumDataContract.cs
369 lines (325 loc) · 15.2 KB
/
EnumDataContract.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.ApplicationServer.Serialization
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Security;
using System.Threading;
using System.Xml;
using Microsoft.ApplicationServer.Common;
internal sealed class EnumDataContract : DataContract
{
[Fx.Tag.SecurityNote(Critical = "Holds instance of CriticalHelper which keeps state that is cached statically for serialization."
+ " Static fields are marked SecurityCritical or readonly to prevent data from being modified or leaked to other components in appdomain.")]
[SecurityCritical]
EnumDataContractCriticalHelper helper;
[Fx.Tag.SecurityNote(Critical = "Initializes SecurityCritical field 'helper'.",
Safe = "Doesn't leak anything.")]
[SecuritySafeCritical]
internal EnumDataContract()
: base(new EnumDataContractCriticalHelper())
{
helper = base.Helper as EnumDataContractCriticalHelper;
}
[Fx.Tag.SecurityNote(Critical = "Initializes SecurityCritical field 'helper'.",
Safe = "Doesn't leak anything.")]
[SecuritySafeCritical]
internal EnumDataContract(Type type)
: base(new EnumDataContractCriticalHelper(type))
{
helper = base.Helper as EnumDataContractCriticalHelper;
}
[Fx.Tag.SecurityNote(Critical = "Accesses SecurityCritical static cache to look up base contract name for a type.",
Safe = "Read only access.")]
[SecuritySafeCritical]
static internal XmlQualifiedName GetBaseContractName(Type type)
{
return EnumDataContractCriticalHelper.GetBaseContractName(type);
}
[Fx.Tag.SecurityNote(Critical = "Accesses SecurityCritical static cache to look up a base contract name.",
Safe = "Read only access.")]
[SecuritySafeCritical]
static internal Type GetBaseType(XmlQualifiedName baseContractName)
{
return EnumDataContractCriticalHelper.GetBaseType(baseContractName);
}
internal XmlQualifiedName BaseContractName
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical BaseContractName property.",
Safe = "BaseContractName only needs to be protected for write.")]
[SecuritySafeCritical]
get { return helper.BaseContractName; }
[Fx.Tag.SecurityNote(Critical = "Sets the critical BaseContractName property.")]
[SecurityCritical]
set { helper.BaseContractName = value; }
}
internal List<DataMember> Members
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical Members property.",
Safe = "Members only needs to be protected for write.")]
[SecuritySafeCritical]
get { return helper.Members; }
[Fx.Tag.SecurityNote(Critical = "Sets the critical Members property.")]
[SecurityCritical]
set { helper.Members = value; }
}
internal List<long> Values
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical Values property.",
Safe = "Values only needs to be protected for write.")]
[SecuritySafeCritical]
get { return helper.Values; }
[Fx.Tag.SecurityNote(Critical = "Sets the critical Values property.")]
[SecurityCritical]
set { helper.Values = value; }
}
internal bool IsFlags
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical IsFlags property.",
Safe = "IsFlags only needs to be protected for write.")]
[SecuritySafeCritical]
get { return helper.IsFlags; }
[Fx.Tag.SecurityNote(Critical = "Sets the critical IsFlags property.")]
[SecurityCritical]
set { helper.IsFlags = value; }
}
internal bool IsULong
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical IsULong property.",
Safe = "IsULong only needs to be protected for write.")]
[SecuritySafeCritical]
get { return helper.IsULong; }
}
XmlDictionaryString[] ChildElementNames
{
[Fx.Tag.SecurityNote(Critical = "Fetches the critical ChildElementNames property.",
Safe = "ChildElementNames only needs to be protected for write.")]
[SecuritySafeCritical]
get { return helper.ChildElementNames; }
}
internal override bool CanContainReferences
{
get { return false; }
}
[Fx.Tag.SecurityNote(Critical = "Holds all state used for (de)serializing enums."
+ " Since the data is cached statically, we lock down access to it.")]
[SecurityCritical]
class EnumDataContractCriticalHelper : DataContract.DataContractCriticalHelper
{
static Dictionary<Type, XmlQualifiedName> typeToName;
static Dictionary<XmlQualifiedName, Type> nameToType;
XmlQualifiedName baseContractName;
List<DataMember> members;
List<long> values;
bool isULong;
bool isFlags;
bool hasDataContract;
XmlDictionaryString[] childElementNames;
static EnumDataContractCriticalHelper()
{
typeToName = new Dictionary<Type, XmlQualifiedName>();
nameToType = new Dictionary<XmlQualifiedName, Type>();
Add(typeof(sbyte), "byte");
Add(typeof(byte), "unsignedByte");
Add(typeof(short), "short");
Add(typeof(ushort), "unsignedShort");
Add(typeof(int), "int");
Add(typeof(uint), "unsignedInt");
Add(typeof(long), "long");
Add(typeof(ulong), "unsignedLong");
}
static internal void Add(Type type, string localName)
{
XmlQualifiedName stableName = CreateQualifiedName(localName, Globals.SchemaNamespace);
typeToName.Add(type, stableName);
nameToType.Add(stableName, type);
}
static internal XmlQualifiedName GetBaseContractName(Type type)
{
XmlQualifiedName retVal = null;
typeToName.TryGetValue(type, out retVal);
return retVal;
}
static internal Type GetBaseType(XmlQualifiedName baseContractName)
{
Type retVal = null;
nameToType.TryGetValue(baseContractName, out retVal);
return retVal;
}
internal EnumDataContractCriticalHelper()
{
IsValueType = true;
}
internal EnumDataContractCriticalHelper(Type type)
: base(type)
{
this.StableName = DataContract.GetStableName(type, out hasDataContract);
Type baseType = Enum.GetUnderlyingType(type);
baseContractName = GetBaseContractName(baseType);
ImportBaseType(baseType);
IsFlags = type.IsDefined(Globals.TypeOfFlagsAttribute, false);
ImportDataMembers();
XmlDictionary dictionary = new XmlDictionary(2 + Members.Count);
Name = dictionary.Add(StableName.Name);
Namespace = dictionary.Add(StableName.Namespace);
childElementNames = new XmlDictionaryString[Members.Count];
for (int i = 0; i < Members.Count; i++)
childElementNames[i] = dictionary.Add(Members[i].Name);
DataContractAttribute dataContractAttribute;
if (TryGetDCAttribute(type, out dataContractAttribute))
{
if (dataContractAttribute.IsReference)
{
DataContract.ThrowInvalidDataContractException(
SR.EnumTypeCannotHaveIsReference(
DataContract.GetClrTypeFullName(type),
dataContractAttribute.IsReference,
false),
type);
}
}
}
internal XmlQualifiedName BaseContractName
{
get
{
return baseContractName;
}
set
{
baseContractName = value;
Type baseType = GetBaseType(baseContractName);
if (baseType == null)
ThrowInvalidDataContractException(SR.InvalidEnumBaseType(value.Name, value.Namespace, StableName.Name, StableName.Namespace));
ImportBaseType(baseType);
}
}
internal List<DataMember> Members
{
get { return members; }
set { members = value; }
}
internal List<long> Values
{
get { return values; }
set { values = value; }
}
internal bool IsFlags
{
get { return isFlags; }
set { isFlags = value; }
}
internal bool IsULong
{
get { return isULong; }
}
internal XmlDictionaryString[] ChildElementNames
{
get { return childElementNames; }
}
void ImportBaseType(Type baseType)
{
isULong = (baseType == Globals.TypeOfULong);
}
void ImportDataMembers()
{
Type type = this.UnderlyingType;
FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
Dictionary<string, DataMember> memberValuesTable = new Dictionary<string, DataMember>();
List<DataMember> tempMembers = new List<DataMember>(fields.Length);
List<long> tempValues = new List<long>(fields.Length);
for (int i = 0; i < fields.Length; i++)
{
FieldInfo field = fields[i];
bool enumMemberValid = false;
if (hasDataContract)
{
object[] memberAttributes = field.GetCustomAttributes(Globals.TypeOfEnumMemberAttribute, false);
if (memberAttributes != null && memberAttributes.Length > 0)
{
if (memberAttributes.Length > 1)
ThrowInvalidDataContractException(SR.TooManyEnumMembers(DataContract.GetClrTypeFullName(field.DeclaringType), field.Name));
EnumMemberAttribute memberAttribute = (EnumMemberAttribute)memberAttributes[0];
DataMember memberContract = new DataMember(field);
// ALTERED_FOR_PORT: Don't have access to .IsSetExplicilty property
if (memberAttribute.Value != null && memberAttribute.Value.Length > 0)
memberContract.Name = memberAttribute.Value;
else
memberContract.Name = field.Name;
ClassDataContract.CheckAndAddMember(tempMembers, memberContract, memberValuesTable);
enumMemberValid = true;
}
object[] dataMemberAttributes = field.GetCustomAttributes(Globals.TypeOfDataMemberAttribute, false);
if (dataMemberAttributes != null && dataMemberAttributes.Length > 0)
ThrowInvalidDataContractException(SR.DataMemberOnEnumField(DataContract.GetClrTypeFullName(field.DeclaringType), field.Name));
}
else
{
if (!field.IsNotSerialized)
{
DataMember memberContract = new DataMember(field);
memberContract.Name = field.Name;
ClassDataContract.CheckAndAddMember(tempMembers, memberContract, memberValuesTable);
enumMemberValid = true;
}
}
if (enumMemberValid)
{
object enumValue = field.GetValue(null);
if (isULong)
tempValues.Add((long)((IConvertible)enumValue).ToUInt64(null));
else
tempValues.Add(((IConvertible)enumValue).ToInt64(null));
}
}
Thread.MemoryBarrier();
members = tempMembers;
values = tempValues;
}
}
internal string GetStringFromEnumValue(long value)
{
if (IsULong)
return XmlConvert.ToString((ulong)value);
else
return XmlConvert.ToString(value);
}
internal override bool Equals(object other, Dictionary<DataContractPairKey, object> checkedContracts)
{
if (IsEqualOrChecked(other, checkedContracts))
return true;
if (base.Equals(other, null))
{
EnumDataContract dataContract = other as EnumDataContract;
if (dataContract != null)
{
if (Members.Count != dataContract.Members.Count || Values.Count != dataContract.Values.Count)
return false;
string[] memberNames1 = new string[Members.Count], memberNames2 = new string[Members.Count];
for (int i = 0; i < Members.Count; i++)
{
memberNames1[i] = Members[i].Name;
memberNames2[i] = dataContract.Members[i].Name;
}
Array.Sort(memberNames1);
Array.Sort(memberNames2);
for (int i = 0; i < Members.Count; i++)
{
if (memberNames1[i] != memberNames2[i])
return false;
}
return (IsFlags == dataContract.IsFlags);
}
}
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}