/
StrongBox.cs
53 lines (47 loc) · 1.68 KB
/
StrongBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Holds a reference to a value.
/// </summary>
/// <typeparam name="T">The type of the value that the <see cref = "StrongBox{T}"></see> references.</typeparam>
public class StrongBox<T> : IStrongBox
{
/// <summary>
/// Gets the strongly typed value associated with the <see cref = "StrongBox{T}"></see>
/// <remarks>This is explicitly exposed as a field instead of a property to enable loading the address of the field.</remarks>
/// </summary>
[MaybeNull] public T Value = default!;
/// <summary>
/// Initializes a new StrongBox which can receive a value when used in a reference call.
/// </summary>
public StrongBox()
{
}
/// <summary>
/// Initializes a new <see cref = "StrongBox{T}"></see> with the specified value.
/// </summary>
/// <param name="value">A value that the <see cref = "StrongBox{T}"></see> will reference.</param>
public StrongBox(T value)
{
Value = value;
}
object? IStrongBox.Value
{
get => Value;
set => Value = (T)value!;
}
}
/// <summary>
/// Defines a property for accessing the value that an object references.
/// </summary>
public interface IStrongBox
{
/// <summary>
/// Gets or sets the value the object references.
/// </summary>
object? Value { get; set; }
}
}