Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Move parts of WeakReference to shared partition (dotnet/coreclr#24800)
Browse files Browse the repository at this point in the history
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
marek-safar authored and stephentoub committed Jun 7, 2019
1 parent e53a463 commit 22a3442
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@
<Compile Include="$(MSBuildThisFileDirectory)System\ValueTuple.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Version.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Void.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\WeakReference.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\WeakReference.T.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Advapi32\Interop.ActivityControl.cs" />
Expand Down
74 changes: 74 additions & 0 deletions src/Common/src/CoreLib/System/WeakReference.T.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;

namespace System
{
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
// This class is sealed to mitigate security issues caused by Object::MemberwiseClone.
public sealed partial class WeakReference<T> : ISerializable
where T : class?
{
// If you fix bugs here, please fix them in WeakReference at the same time.

// Creates a new WeakReference that keeps track of target.
// Assumes a Short Weak Reference (ie TrackResurrection is false.)
//
public WeakReference(T target)
: this(target, false)
{
}

//Creates a new WeakReference that keeps track of target.
//
public WeakReference(T target, bool trackResurrection)
{
Create(target, trackResurrection);
}

internal WeakReference(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

T target = (T)info.GetValue("TrackedObject", typeof(T))!; // Do not rename (binary serialization)
bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization)

Create(target, trackResurrection);
}

//
// We are exposing TryGetTarget instead of a simple getter to avoid a common problem where people write incorrect code like:
//
// WeakReference ref = ...;
// if (ref.Target != null)
// DoSomething(ref.Target)
//
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
public bool TryGetTarget([NotNullWhen(true)] out T target)
{
// Call the worker method that has more performant but less user friendly signature.
T o = this.Target;
target = o;
return o != null;
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

info.AddValue("TrackedObject", this.Target, typeof(T)); // Do not rename (binary serialization)
info.AddValue("TrackResurrection", IsTrackResurrection()); // Do not rename (binary serialization)
}
}
}
51 changes: 51 additions & 0 deletions src/Common/src/CoreLib/System/WeakReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.Serialization;

namespace System
{
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public partial class WeakReference : ISerializable
{
// If you fix bugs here, please fix them in WeakReference<T> at the same time.

// Creates a new WeakReference that keeps track of target.
// Assumes a Short Weak Reference (ie TrackResurrection is false.)
//
public WeakReference(object? target)
: this(target, false)
{
}

public WeakReference(object? target, bool trackResurrection)
{
Create(target, trackResurrection);
}

protected WeakReference(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

object? target = info.GetValue("TrackedObject", typeof(object)); // Do not rename (binary serialization)
bool trackResurrection = info.GetBoolean("TrackResurrection"); // Do not rename (binary serialization)

Create(target, trackResurrection);
}

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}
info.AddValue("TrackedObject", Target, typeof(object)); // Do not rename (binary serialization)
info.AddValue("TrackResurrection", IsTrackResurrection()); // Do not rename (binary serialization)
}
}
}

0 comments on commit 22a3442

Please sign in to comment.