-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RetrievePlatformValueRequest.cs
49 lines (42 loc) · 1.25 KB
/
RetrievePlatformValueRequest.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
#nullable enable
using System;
namespace Microsoft.Maui
{
/// <summary>
/// Specifies a request for the retrieval of a platform value.
/// </summary>
public class RetrievePlatformValueRequest<T>
{
bool _hasResult;
T? _result;
/// <summary>
/// The result of the retrieval operation.
/// </summary>
public T Result => _hasResult ? _result! : throw new InvalidOperationException("No result value was set.");
/// <summary>
/// Sets the result value of the retrieval operation.
/// </summary>
/// <param name="result">The result value to set on this request.</param>
/// <exception cref="InvalidOperationException">The result has already been set.</exception>
public void SetResult(T result)
{
if (_hasResult)
throw new InvalidOperationException("Request already had a result value set.");
_result = result;
_hasResult = true;
}
/// <summary>
/// Attempts to set the result value of the retrieval operation.
/// </summary>
/// <param name="result">The result value to set on this request.</param>
/// <returns>true if the operation was successful; otherwise, false.</returns>
public bool TrySetResult(T result)
{
if (_hasResult)
return false;
_result = result;
_hasResult = true;
return true;
}
}
}