Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 641d6cd

Browse files
committed
first attempt: Keep the target of WeakReference alive, otherwise the produced output may be different.
1 parent 0806470 commit 641d6cd

File tree

1 file changed

+5
-0
lines changed
  • src/libraries/System.Resources.Extensions/tests/BinaryFormatTests/FormattedObject

1 file changed

+5
-0
lines changed

src/libraries/System.Resources.Extensions/tests/BinaryFormatTests/FormattedObject/BasicObjectTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class BasicObjectTests : Common.BasicObjectTests<FormattedObjectSerialize
1414
[MemberData(nameof(SerializableObjects))]
1515
public void BasicObjectsRoundTripAndMatch(object value, TypeSerializableValue[] _)
1616
{
17+
// Keep the target of WeakReference alive, otherwise the produced output may be different.
18+
object keepAlive = value is WeakReference weakReference ? weakReference.Target : null;
19+
1720
// We need to round trip through the BinaryFormatter as a few objects in tests remove
1821
// serialized data on deserialization.
1922
BinaryFormatter formatter = new();
@@ -38,5 +41,7 @@ public void BasicObjectsRoundTripAndMatch(object value, TypeSerializableValue[]
3841

3942
// Now compare the two streams to ensure they are identical
4043
Assert.Equal(serialized.Length, deserializedSerialized.Length);
44+
45+
GC.KeepAlive(keepAlive);
4146
}
4247
}

0 commit comments

Comments
 (0)