Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit cf98cbd

Browse files
committed
Add Debug.Assert in all places that access type members through reflection
1 parent 52031ef commit cf98cbd

File tree

5 files changed

+238
-12
lines changed

5 files changed

+238
-12
lines changed

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/BitFlagsGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System;
5+
using System.Reflection;
6+
using System.Reflection.Emit;
7+
using System.Security;
8+
49
namespace System.Runtime.Serialization
510
{
6-
using System;
7-
using System.Reflection;
8-
using System.Reflection.Emit;
9-
using System.Security;
10-
1111
internal class BitFlagsGenerator
1212
{
1313
private int _bitCount;

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ private static MethodInfo GetTypeFromHandle
3333
get
3434
{
3535
if (s_getTypeFromHandle == null)
36+
{
3637
s_getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");
38+
Debug.Assert(s_getTypeFromHandle != null);
39+
}
3740
return s_getTypeFromHandle;
3841
}
3942
}
@@ -54,7 +57,10 @@ private static MethodInfo ObjectEquals
5457
get
5558
{
5659
if (s_objectEquals == null)
60+
{
5761
s_objectEquals = Globals.TypeOfObject.GetMethod("Equals", BindingFlags.Public | BindingFlags.Static);
62+
Debug.Assert(s_objectEquals != null);
63+
}
5864
return s_objectEquals;
5965
}
6066
}
@@ -74,7 +80,10 @@ private static MethodInfo ArraySetValue
7480
get
7581
{
7682
if (s_arraySetValue == null)
83+
{
7784
s_arraySetValue = typeof(Array).GetMethod("SetValue", new Type[] { typeof(object), typeof(int) });
85+
Debug.Assert(s_arraySetValue != null);
86+
}
7887
return s_arraySetValue;
7988
}
8089
}
@@ -88,7 +97,10 @@ private static MethodInfo ObjectToString
8897
get
8998
{
9099
if (s_objectToString == null)
100+
{
91101
s_objectToString = typeof(object).GetMethod("ToString", Array.Empty<Type>());
102+
Debug.Assert(s_objectToString != null);
103+
}
92104
return s_objectToString;
93105
}
94106
}
@@ -100,7 +112,10 @@ private static MethodInfo StringFormat
100112
get
101113
{
102114
if (s_stringFormat == null)
115+
{
103116
s_stringFormat = typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(object[]) });
117+
Debug.Assert(s_stringFormat != null);
118+
}
104119
return s_stringFormat;
105120
}
106121
}

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/DataContractSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal DataContractSerializer(Type type, IEnumerable<Type> knownTypes, int max
7474
Initialize(type, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, preserveObjectReferences, null, false);
7575
}
7676
#endif
77-
77+
7878
public DataContractSerializer(Type type, XmlDictionaryString rootName, XmlDictionaryString rootNamespace,
7979
IEnumerable<Type> knownTypes,
8080
int maxItemsInObjectGraph,

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatGeneratorStatics.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public static MethodInfo GetCurrentMethod
125125
if (s_ienumeratorGetCurrentMethod == null)
126126
{
127127
s_ienumeratorGetCurrentMethod = typeof(IEnumerator).GetProperty("Current").GetGetMethod();
128+
Debug.Assert(s_ienumeratorGetCurrentMethod != null);
128129
}
129130
return s_ienumeratorGetCurrentMethod;
130131
}
@@ -137,6 +138,7 @@ public static MethodInfo GetItemContractMethod
137138
if (s_getItemContractMethod == null)
138139
{
139140
s_getItemContractMethod = typeof(CollectionDataContract).GetProperty("ItemContract", Globals.ScanAllMembers).GetGetMethod(true); // nonPublic
141+
Debug.Assert(s_getItemContractMethod != null);
140142
}
141143
return s_getItemContractMethod;
142144
}
@@ -149,6 +151,7 @@ public static MethodInfo GetJsonDataContractMethod
149151
if (s_getJsonDataContractMethod == null)
150152
{
151153
s_getJsonDataContractMethod = typeof(JsonDataContract).GetMethod("GetJsonDataContract", Globals.ScanAllMembers);
154+
Debug.Assert(s_getJsonDataContractMethod != null);
152155
}
153156
return s_getJsonDataContractMethod;
154157
}
@@ -161,6 +164,7 @@ public static MethodInfo GetJsonMemberIndexMethod
161164
if (s_getJsonMemberIndexMethod == null)
162165
{
163166
s_getJsonMemberIndexMethod = typeof(XmlObjectSerializerReadContextComplexJson).GetMethod("GetJsonMemberIndex", Globals.ScanAllMembers);
167+
Debug.Assert(s_getJsonMemberIndexMethod != null);
164168
}
165169
return s_getJsonMemberIndexMethod;
166170
}
@@ -173,6 +177,7 @@ public static MethodInfo GetRevisedItemContractMethod
173177
if (s_getRevisedItemContractMethod == null)
174178
{
175179
s_getRevisedItemContractMethod = typeof(XmlObjectSerializerWriteContextComplexJson).GetMethod("GetRevisedItemContract", Globals.ScanAllMembers);
180+
Debug.Assert(s_getRevisedItemContractMethod != null);
176181
}
177182
return s_getRevisedItemContractMethod;
178183
}
@@ -185,6 +190,7 @@ public static MethodInfo GetUninitializedObjectMethod
185190
if (s_getUninitializedObjectMethod == null)
186191
{
187192
s_getUninitializedObjectMethod = typeof(XmlFormatReaderGenerator).GetMethod("UnsafeGetUninitializedObject", Globals.ScanAllMembers, new Type[] { typeof(Type) });
193+
Debug.Assert(s_getUninitializedObjectMethod != null);
188194
}
189195
return s_getUninitializedObjectMethod;
190196
}
@@ -197,7 +203,8 @@ public static MethodInfo IsStartElementMethod0
197203
{
198204
if (s_isStartElementMethod0 == null)
199205
{
200-
s_isStartElementMethod0 = typeof (XmlReaderDelegator).GetMethod("IsStartElement", Globals.ScanAllMembers, Array.Empty<Type>());
206+
s_isStartElementMethod0 = typeof(XmlReaderDelegator).GetMethod("IsStartElement", Globals.ScanAllMembers, Array.Empty<Type>());
207+
Debug.Assert(s_isStartElementMethod0 != null);
201208
}
202209
return s_isStartElementMethod0;
203210
}
@@ -210,6 +217,7 @@ public static MethodInfo IsStartElementMethod2
210217
if (s_isStartElementMethod2 == null)
211218
{
212219
s_isStartElementMethod2 = typeof(XmlReaderDelegator).GetMethod("IsStartElement", Globals.ScanAllMembers, new Type[] { typeof(XmlDictionaryString), typeof(XmlDictionaryString) });
220+
Debug.Assert(s_isStartElementMethod2 != null);
213221
}
214222
return s_isStartElementMethod2;
215223
}
@@ -222,6 +230,7 @@ public static PropertyInfo LocalNameProperty
222230
if (s_localNameProperty == null)
223231
{
224232
s_localNameProperty = typeof(XmlReaderDelegator).GetProperty("LocalName", Globals.ScanAllMembers);
233+
Debug.Assert(s_localNameProperty != null);
225234
}
226235
return s_localNameProperty;
227236
}
@@ -234,6 +243,7 @@ public static PropertyInfo NamespaceProperty
234243
if (s_namespaceProperty == null)
235244
{
236245
s_namespaceProperty = typeof(XmlReaderDelegator).GetProperty("NamespaceProperty", Globals.ScanAllMembers);
246+
Debug.Assert(s_namespaceProperty != null);
237247
}
238248
return s_namespaceProperty;
239249
}
@@ -246,6 +256,7 @@ public static MethodInfo MoveNextMethod
246256
if (s_ienumeratorMoveNextMethod == null)
247257
{
248258
s_ienumeratorMoveNextMethod = typeof(IEnumerator).GetMethod("MoveNext");
259+
Debug.Assert(s_ienumeratorMoveNextMethod != null);
249260
}
250261
return s_ienumeratorMoveNextMethod;
251262
}
@@ -258,6 +269,7 @@ public static MethodInfo MoveToContentMethod
258269
if (s_moveToContentMethod == null)
259270
{
260271
s_moveToContentMethod = typeof(XmlReaderDelegator).GetMethod("MoveToContent", Globals.ScanAllMembers);
272+
Debug.Assert(s_moveToContentMethod != null);
261273
}
262274
return s_moveToContentMethod;
263275
}
@@ -270,6 +282,7 @@ public static PropertyInfo NodeTypeProperty
270282
if (s_nodeTypeProperty == null)
271283
{
272284
s_nodeTypeProperty = typeof(XmlReaderDelegator).GetProperty("NodeType", Globals.ScanAllMembers);
285+
Debug.Assert(s_nodeTypeProperty != null);
273286
}
274287
return s_nodeTypeProperty;
275288
}
@@ -282,6 +295,7 @@ public static MethodInfo ReadJsonValueMethod
282295
if (s_readJsonValueMethod == null)
283296
{
284297
s_readJsonValueMethod = typeof(DataContractJsonSerializer).GetMethod("ReadJsonValue", Globals.ScanAllMembers);
298+
Debug.Assert(s_readJsonValueMethod != null);
285299
}
286300
return s_readJsonValueMethod;
287301
}
@@ -306,6 +320,7 @@ public static MethodInfo ThrowDuplicateMemberExceptionMethod
306320
if (s_throwDuplicateMemberExceptionMethod == null)
307321
{
308322
s_throwDuplicateMemberExceptionMethod = typeof(XmlObjectSerializerReadContextComplexJson).GetMethod("ThrowDuplicateMemberException", Globals.ScanAllMembers);
323+
Debug.Assert(s_throwDuplicateMemberExceptionMethod != null);
309324
}
310325
return s_throwDuplicateMemberExceptionMethod;
311326
}
@@ -318,6 +333,7 @@ public static MethodInfo ThrowMissingRequiredMembersMethod
318333
if (s_throwMissingRequiredMembersMethod == null)
319334
{
320335
s_throwMissingRequiredMembersMethod = typeof(XmlObjectSerializerReadContextComplexJson).GetMethod("ThrowMissingRequiredMembers", Globals.ScanAllMembers);
336+
Debug.Assert(s_throwMissingRequiredMembersMethod != null);
321337
}
322338
return s_throwMissingRequiredMembersMethod;
323339
}
@@ -330,6 +346,7 @@ public static PropertyInfo TypeHandleProperty
330346
if (s_typeHandleProperty == null)
331347
{
332348
s_typeHandleProperty = typeof(Type).GetProperty("TypeHandle");
349+
Debug.Assert(s_typeHandleProperty != null);
333350
}
334351
return s_typeHandleProperty;
335352
}
@@ -342,6 +359,7 @@ public static PropertyInfo UseSimpleDictionaryFormatReadProperty
342359
if (s_useSimpleDictionaryFormatReadProperty == null)
343360
{
344361
s_useSimpleDictionaryFormatReadProperty = typeof(XmlObjectSerializerReadContextComplexJson).GetProperty("UseSimpleDictionaryFormat", Globals.ScanAllMembers);
362+
Debug.Assert(s_useSimpleDictionaryFormatReadProperty != null);
345363
}
346364
return s_useSimpleDictionaryFormatReadProperty;
347365
}
@@ -354,6 +372,7 @@ public static PropertyInfo UseSimpleDictionaryFormatWriteProperty
354372
if (s_useSimpleDictionaryFormatWriteProperty == null)
355373
{
356374
s_useSimpleDictionaryFormatWriteProperty = typeof(XmlObjectSerializerWriteContextComplexJson).GetProperty("UseSimpleDictionaryFormat", Globals.ScanAllMembers);
375+
Debug.Assert(s_useSimpleDictionaryFormatWriteProperty != null);
357376
}
358377
return s_useSimpleDictionaryFormatWriteProperty;
359378
}
@@ -366,6 +385,7 @@ public static MethodInfo WriteAttributeStringMethod
366385
if (s_writeAttributeStringMethod == null)
367386
{
368387
s_writeAttributeStringMethod = typeof(XmlWriterDelegator).GetMethod("WriteAttributeString", Globals.ScanAllMembers, new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });
388+
Debug.Assert(s_writeAttributeStringMethod != null);
369389
}
370390
return s_writeAttributeStringMethod;
371391
}
@@ -378,6 +398,7 @@ public static MethodInfo WriteEndElementMethod
378398
if (s_writeEndElementMethod == null)
379399
{
380400
s_writeEndElementMethod = typeof(XmlWriterDelegator).GetMethod("WriteEndElement", Globals.ScanAllMembers, Array.Empty<Type>());
401+
Debug.Assert(s_writeEndElementMethod != null);
381402
}
382403
return s_writeEndElementMethod;
383404
}
@@ -390,6 +411,7 @@ public static MethodInfo WriteJsonISerializableMethod
390411
if (s_writeJsonISerializableMethod == null)
391412
{
392413
s_writeJsonISerializableMethod = typeof(XmlObjectSerializerWriteContextComplexJson).GetMethod("WriteJsonISerializable", Globals.ScanAllMembers);
414+
Debug.Assert(s_writeJsonISerializableMethod != null);
393415
}
394416
return s_writeJsonISerializableMethod;
395417
}
@@ -402,6 +424,7 @@ public static MethodInfo WriteJsonNameWithMappingMethod
402424
if (s_writeJsonNameWithMappingMethod == null)
403425
{
404426
s_writeJsonNameWithMappingMethod = typeof(XmlObjectSerializerWriteContextComplexJson).GetMethod("WriteJsonNameWithMapping", Globals.ScanAllMembers);
427+
Debug.Assert(s_writeJsonNameWithMappingMethod != null);
405428
}
406429
return s_writeJsonNameWithMappingMethod;
407430
}
@@ -414,6 +437,7 @@ public static MethodInfo WriteJsonValueMethod
414437
if (s_writeJsonValueMethod == null)
415438
{
416439
s_writeJsonValueMethod = typeof(DataContractJsonSerializer).GetMethod("WriteJsonValue", Globals.ScanAllMembers);
440+
Debug.Assert(s_writeJsonValueMethod != null);
417441
}
418442
return s_writeJsonValueMethod;
419443
}
@@ -426,6 +450,7 @@ public static MethodInfo WriteStartElementMethod
426450
if (s_writeStartElementMethod == null)
427451
{
428452
s_writeStartElementMethod = typeof(XmlWriterDelegator).GetMethod("WriteStartElement", Globals.ScanAllMembers, new Type[] { typeof(XmlDictionaryString), typeof(XmlDictionaryString) });
453+
Debug.Assert(s_writeStartElementMethod != null);
429454
}
430455
return s_writeStartElementMethod;
431456
}
@@ -439,6 +464,7 @@ public static MethodInfo WriteStartElementStringMethod
439464
if (s_writeStartElementStringMethod == null)
440465
{
441466
s_writeStartElementStringMethod = typeof(XmlWriterDelegator).GetMethod("WriteStartElement", Globals.ScanAllMembers, new Type[] { typeof(string), typeof(string) });
467+
Debug.Assert(s_writeStartElementStringMethod != null);
442468
}
443469
return s_writeStartElementStringMethod;
444470
}
@@ -465,6 +491,7 @@ public static MethodInfo GetJsonMemberNameMethod
465491
if (s_getJsonMemberNameMethod == null)
466492
{
467493
s_getJsonMemberNameMethod = typeof(XmlObjectSerializerReadContextComplexJson).GetMethod("GetJsonMemberName", Globals.ScanAllMembers, new Type[] { typeof(XmlReaderDelegator) });
494+
Debug.Assert(s_getJsonMemberNameMethod != null);
468495
}
469496
return s_getJsonMemberNameMethod;
470497
}

0 commit comments

Comments
 (0)