Skip to content

Commit f0c20fc

Browse files
committed
fix compiler errors caused by lack of global usings and supporting older TFMs
1 parent ba62c25 commit f0c20fc

24 files changed

+172
-150
lines changed

src/libraries/System.Resources.Extensions/src/BinaryFormat/BinaryFormattedObject.IParseState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
using System.IO;
46
using System.Runtime.Serialization.BinaryFormat;
57

68
namespace System.Windows.Forms.BinaryFormat;

src/libraries/System.Resources.Extensions/src/BinaryFormat/BinaryFormattedObject.ITypeResolver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Reflection.Metadata;
56

67
namespace System.Windows.Forms.BinaryFormat;

src/libraries/System.Resources.Extensions/src/BinaryFormat/BinaryFormattedObject.ParseState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
using System.IO;
46
using System.Runtime.Serialization.BinaryFormat;
57

68
namespace System.Windows.Forms.BinaryFormat;

src/libraries/System.Resources.Extensions/src/BinaryFormat/BinaryFormattedObject.TypeResolver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.IO;
48
using System.Reflection;
59
using System.Reflection.Metadata;
610
using System.Runtime.Serialization;

src/libraries/System.Resources.Extensions/src/BinaryFormat/BinaryFormattedObject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.IO;
47
using System.Reflection;
58
using System.Runtime.ExceptionServices;
69
using System.Runtime.Serialization;

src/libraries/System.Resources.Extensions/src/BinaryFormat/BinaryFormattedObjectExtensions.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
58
using System.Drawing;
69
using System.Runtime.Serialization.BinaryFormat;
10+
using System.Windows.Forms.BinaryFormat.Deserializer;
711

812
namespace System.Windows.Forms.BinaryFormat;
913

@@ -162,22 +166,22 @@ static bool Get(BinaryFormattedObject format, [NotNullWhen(true)] out object? li
162166
// BinaryFormatter serializes the entire backing array, so we need to trim it down to the size of the list.
163167
list = arrayRecord switch
164168
{
165-
ArrayRecord<string> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
166-
ArrayRecord<bool> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
167-
ArrayRecord<byte> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
168-
ArrayRecord<sbyte> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
169-
ArrayRecord<char> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
170-
ArrayRecord<short> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
171-
ArrayRecord<ushort> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
172-
ArrayRecord<int> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
173-
ArrayRecord<uint> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
174-
ArrayRecord<long> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
175-
ArrayRecord<ulong> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
176-
ArrayRecord<float> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
177-
ArrayRecord<double> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
178-
ArrayRecord<decimal> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
179-
ArrayRecord<TimeSpan> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
180-
ArrayRecord<DateTime> ar => ar.ToArray(maxLength: Array.MaxLength).CreateTrimmedList(size),
169+
ArrayRecord<string> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
170+
ArrayRecord<bool> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
171+
ArrayRecord<byte> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
172+
ArrayRecord<sbyte> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
173+
ArrayRecord<char> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
174+
ArrayRecord<short> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
175+
ArrayRecord<ushort> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
176+
ArrayRecord<int> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
177+
ArrayRecord<uint> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
178+
ArrayRecord<long> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
179+
ArrayRecord<ulong> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
180+
ArrayRecord<float> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
181+
ArrayRecord<double> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
182+
ArrayRecord<decimal> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
183+
ArrayRecord<TimeSpan> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
184+
ArrayRecord<DateTime> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength).CreateTrimmedList(size),
181185
_ => throw new InvalidOperationException()
182186
};
183187

@@ -241,22 +245,22 @@ static bool Get(BinaryFormattedObject format, [NotNullWhen(true)] out object? va
241245

242246
value = format.RootRecord switch
243247
{
244-
ArrayRecord<string> ar => ar.ToArray(maxLength: Array.MaxLength),
245-
ArrayRecord<bool> ar => ar.ToArray(maxLength: Array.MaxLength),
246-
ArrayRecord<byte> ar => ar.ToArray(maxLength: Array.MaxLength),
247-
ArrayRecord<sbyte> ar => ar.ToArray(maxLength: Array.MaxLength),
248-
ArrayRecord<char> ar => ar.ToArray(maxLength: Array.MaxLength),
249-
ArrayRecord<short> ar => ar.ToArray(maxLength: Array.MaxLength),
250-
ArrayRecord<ushort> ar => ar.ToArray(maxLength: Array.MaxLength),
251-
ArrayRecord<int> ar => ar.ToArray(maxLength: Array.MaxLength),
252-
ArrayRecord<uint> ar => ar.ToArray(maxLength: Array.MaxLength),
253-
ArrayRecord<long> ar => ar.ToArray(maxLength: Array.MaxLength),
254-
ArrayRecord<ulong> ar => ar.ToArray(maxLength: Array.MaxLength),
255-
ArrayRecord<float> ar => ar.ToArray(maxLength: Array.MaxLength),
256-
ArrayRecord<double> ar => ar.ToArray(maxLength: Array.MaxLength),
257-
ArrayRecord<decimal> ar => ar.ToArray(maxLength: Array.MaxLength),
258-
ArrayRecord<TimeSpan> ar => ar.ToArray(maxLength: Array.MaxLength),
259-
ArrayRecord<DateTime> ar => ar.ToArray(maxLength: Array.MaxLength),
248+
ArrayRecord<string> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
249+
ArrayRecord<bool> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
250+
ArrayRecord<byte> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
251+
ArrayRecord<sbyte> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
252+
ArrayRecord<char> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
253+
ArrayRecord<short> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
254+
ArrayRecord<ushort> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
255+
ArrayRecord<int> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
256+
ArrayRecord<uint> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
257+
ArrayRecord<long> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
258+
ArrayRecord<ulong> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
259+
ArrayRecord<float> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
260+
ArrayRecord<double> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
261+
ArrayRecord<decimal> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
262+
ArrayRecord<TimeSpan> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
263+
ArrayRecord<DateTime> ar => ar.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength),
260264
_ => throw new InvalidOperationException()
261265
};
262266

@@ -300,8 +304,8 @@ static bool Get(BinaryFormattedObject format, [NotNullWhen(true)] out object? ha
300304
}
301305

302306
Hashtable temp = new((int)keysRecord.Length);
303-
object?[] keys = keysRecord.ToArray(maxLength: Array.MaxLength);
304-
object?[] values = valuesRecord.ToArray(maxLength: Array.MaxLength);
307+
object?[] keys = keysRecord.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength);
308+
object?[] values = valuesRecord.ToArray(maxLength: ArrayRecordDeserializer.MaxArrayLength);
305309
for (int i = 0; i < keys.Length; i++)
306310
{
307311
object? key = keys[i];

src/libraries/System.Resources.Extensions/src/BinaryFormat/Deserializer/ArrayRecordDeserializer.cs

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
47
using System.Runtime.Serialization.BinaryFormat;
58

69
namespace System.Windows.Forms.BinaryFormat.Deserializer;
710

811
internal sealed class ArrayRecordDeserializer : ObjectRecordDeserializer
912
{
13+
internal const int MaxArrayLength = 2147483591;
14+
1015
private readonly ArrayRecord _arrayRecord;
1116
private readonly Type _elementType;
1217
private readonly Array _arrayOfClassRecords;
1318
private readonly Array _arrayOfT;
14-
private int _index;
15-
private bool _hasFixups;
19+
private readonly int[] _lengths, _indices;
20+
private bool _hasFixups, _canIterate;
1621

1722
[RequiresUnreferencedCode("Calls System.Windows.Forms.BinaryFormat.BinaryFormattedObject.TypeResolver.GetType(TypeName)")]
1823
internal ArrayRecordDeserializer(ArrayRecord arrayRecord, IDeserializer deserializer)
@@ -31,7 +36,7 @@ internal ArrayRecordDeserializer(ArrayRecord arrayRecord, IDeserializer deserial
3136
};
3237
// Tricky part: for arrays of classes/structs the following record allocates and array of class records
3338
// (because the payload reader can not load types, instantiate objects and rehydrate them)
34-
_arrayOfClassRecords = arrayRecord.ToArray(expectedArrayType, maxLength: Array.MaxLength);
39+
_arrayOfClassRecords = arrayRecord.ToArray(expectedArrayType, maxLength: MaxArrayLength);
3540
// Now we need to create an array of the same length, but of a different, exact type
3641
Type elementType = _arrayOfClassRecords.GetType();
3742
while (elementType.IsArray)
@@ -46,13 +51,18 @@ internal ArrayRecordDeserializer(ArrayRecord arrayRecord, IDeserializer deserial
4651
}
4752

4853
Object = _arrayOfT = Array.CreateInstance(_elementType, lengths);
54+
_lengths = lengths;
55+
_indices = new int[lengths.Length];
4956
}
5057

5158
internal override Id Continue()
5259
{
53-
while (_index < _arrayRecord.Length)
60+
int[] indices = _indices;
61+
int[] lengths = _lengths;
62+
63+
while (_canIterate)
5464
{
55-
(object? memberValue, Id reference) = UnwrapMemberValue(_arrayOfClassRecords.GetArrayData<object?>()[_index]);
65+
(object? memberValue, Id reference) = UnwrapMemberValue(_arrayOfClassRecords.GetValue(indices));
5666

5767
if (s_missingValueSentinel == memberValue)
5868
{
@@ -64,27 +74,28 @@ internal override Id Continue()
6474
{
6575
// Need to track a fixup for this index.
6676
_hasFixups = true;
67-
Deserializer.PendValueUpdater(new ArrayUpdater(_arrayRecord.ObjectId, reference, _index));
77+
Deserializer.PendValueUpdater(new ArrayUpdater(_arrayRecord.ObjectId, reference, indices.ToArray()));
6878
}
6979

70-
if (memberValue is null && !_elementType.IsValueType)
71-
{
72-
// No point in setting a null. If we're a value type, let it flow to throw when setting.
73-
_index++;
74-
continue;
75-
}
80+
_arrayOfT.SetValue(memberValue, indices);
7681

77-
if (_elementType.IsValueType)
82+
int dimension = indices.Length - 1;
83+
while (dimension >= 0)
7884
{
79-
_arrayOfT.SetArrayValueByFlattenedIndex(memberValue, _index);
85+
indices[dimension]++;
86+
if (indices[dimension] < lengths[dimension])
87+
{
88+
break;
89+
}
90+
91+
indices[dimension] = 0;
92+
dimension--;
8093
}
81-
else
94+
95+
if (dimension < 0)
8296
{
83-
Span<object?> flatSpan = _arrayOfT.GetArrayData<object?>();
84-
flatSpan[_index] = memberValue;
97+
_canIterate = false;
8598
}
86-
87-
_index++;
8899
}
89100

90101
// No more missing member refs.
@@ -99,21 +110,21 @@ internal override Id Continue()
99110

100111
internal static Array GetArraySinglePrimitive(SerializationRecord record) => record switch
101112
{
102-
ArrayRecord<bool> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
103-
ArrayRecord<byte> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
104-
ArrayRecord<sbyte> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
105-
ArrayRecord<char> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
106-
ArrayRecord<short> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
107-
ArrayRecord<ushort> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
108-
ArrayRecord<int> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
109-
ArrayRecord<uint> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
110-
ArrayRecord<long> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
111-
ArrayRecord<ulong> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
112-
ArrayRecord<float> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
113-
ArrayRecord<double> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
114-
ArrayRecord<decimal> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
115-
ArrayRecord<DateTime> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
116-
ArrayRecord<TimeSpan> primitiveArray => primitiveArray.ToArray(maxLength: Array.MaxLength),
113+
ArrayRecord<bool> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
114+
ArrayRecord<byte> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
115+
ArrayRecord<sbyte> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
116+
ArrayRecord<char> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
117+
ArrayRecord<short> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
118+
ArrayRecord<ushort> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
119+
ArrayRecord<int> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
120+
ArrayRecord<uint> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
121+
ArrayRecord<long> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
122+
ArrayRecord<ulong> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
123+
ArrayRecord<float> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
124+
ArrayRecord<double> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
125+
ArrayRecord<decimal> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
126+
ArrayRecord<DateTime> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
127+
ArrayRecord<TimeSpan> primitiveArray => primitiveArray.ToArray(maxLength: MaxArrayLength),
117128
_ => throw new NotSupportedException(),
118129
};
119130

@@ -144,7 +155,7 @@ internal override Id Continue()
144155
_ => arrayRecordElementType.MakeArrayType()
145156
};
146157

147-
return arrayRecord.ToArray(expectedArrayType, maxLength: Array.MaxLength);
158+
return arrayRecord.ToArray(expectedArrayType, maxLength: MaxArrayLength);
148159

149160
static bool HasBuiltInSupport(Type elementType)
150161
=> elementType == typeof(string)
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
46
namespace System.Windows.Forms.BinaryFormat.Deserializer;
57

68
internal sealed class ArrayUpdater : ValueUpdater
79
{
8-
private readonly int _index;
10+
private readonly int[] _indices;
911

10-
internal ArrayUpdater(int objectId, int valueId, int index) : base(objectId, valueId)
12+
internal ArrayUpdater(int objectId, int valueId, int[] indices) : base(objectId, valueId)
1113
{
12-
_index = index;
14+
_indices = indices;
1315
}
1416

1517
internal override void UpdateValue(IDictionary<int, object> objects)
1618
{
1719
object value = objects[ValueId];
1820
Array array = (Array)objects[ObjectId];
19-
array.SetArrayValueByFlattenedIndex(value, _index);
21+
array.SetValue(value, _indices);
2022
}
2123
}

src/libraries/System.Resources.Extensions/src/BinaryFormat/Deserializer/ClassRecordDeserializer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Runtime.CompilerServices;
56
using System.Runtime.Serialization;
67
using System.Runtime.Serialization.BinaryFormat;
@@ -40,7 +41,13 @@ internal static ObjectRecordDeserializer Create(ClassRecord classRecord, IDeseri
4041
throw new SerializationException($"Type '{type}' is not marked as serializable.");
4142
}
4243

43-
object @object = RuntimeHelpers.GetUninitializedObject(type);
44+
object @object =
45+
#if NETCOREAPP
46+
RuntimeHelpers.GetUninitializedObject(type);
47+
#else
48+
// adsitnik: is this the best option we have?
49+
Activator.CreateInstance(type);
50+
#endif
4451

4552
// Invoke any OnDeserializing methods.
4653
SerializationEvents.GetOnDeserializingForType(type, @object)?.Invoke(deserializer.Options.StreamingContext);

0 commit comments

Comments
 (0)