mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-16 17:10:20 -10:00
* Remove unnecessary asserts * Make several classes static * Use is instead of as+null check * Use T? instead of Nullable<T> * more static classes * Mark locals as const * Merge declaration and initialization of some variables * Remove unnecessary casts * Remove unnecessary "unsafe"s * Simplify several lambda expressions * Remove redundant parentheses * Remove redundant '== true' * Remove redundant empty lines * Simplify boolean comparison with '== false' * Replace if-statement with return statement * Use while for infinite loop * Add static to all partial static class declarations * Use ++/-- operator instead of assignment * Use string.IsNullOrEmpty * Use coalesce expression * Simplify lazy initialization * Use coalese expression * Join string expressions * Use regular string literal instead of verbatim string literal * Optimize StringBuilder.Append calls * Remove redundant assignment * Remove unnecessary unsafe context * Merge processor directives * Use String.Equals instead of String.Compare * Use Debug.Fail instead of Debug.Assert(false * Remove Attribute suffix * Use predefined type * Use compound assignment * Use while statement to create an infinite loop * Remove redundant base ctor call * Avoid using catch (Exception) * Remove empty regions * Span comparison to null * Avoid unnecessary boxing of value type * Expression is always equal to 'true' * Remove unused method * update coalesce assignment * fix unsafe * fix redundant parens * Fix whitespace errors introduced Signed-off-by: dotnet-bot <[email protected]>
75 lines
2.7 KiB
C#
75 lines
2.7 KiB
C#
// 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);
|
|
}
|
|
|
|
private 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)
|
|
//
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool TryGetTarget([MaybeNullWhen(false), 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)
|
|
}
|
|
}
|
|
}
|