forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilderExtensions.cs
84 lines (77 loc) · 3.29 KB
/
BuilderExtensions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2022 .NET Foundation and Contributors. All rights reserved.
// 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 full license information.
using System;
using System.Collections.Generic;
namespace IntegrationTests.Shared.Tests
{
/// <summary>
/// Extension methods that assist with builder type operations.
/// </summary>
public static class BuilderExtensions
{
/// <summary>
/// Sets a field to the specified value.
/// </summary>
/// <typeparam name="TBuilder">The type of builder.</typeparam>
/// <typeparam name="TField">The type of field.</typeparam>
/// <param name="this">The builder instance to use.</param>
/// <param name="field">The reference to the field we are setting.</param>
/// <param name="value">The new value of the field.</param>
/// <returns>The builder instance.</returns>
public static TBuilder With<TBuilder, TField>(this TBuilder @this, ref TField field, TField value)
where TBuilder : IBuilder
{
field = value;
return @this;
}
/// <summary>
/// Sets a field to the specified enumerable value.
/// It will add the values to the specified list, and won't override existing values.
/// </summary>
/// <typeparam name="TBuilder">The type of builder.</typeparam>
/// <typeparam name="TField">The type of field.</typeparam>
/// <param name="this">The builder instance to use.</param>
/// <param name="field">The reference to the list field we are setting.</param>
/// <param name="values">The new values of the field.</param>
/// <returns>The builder instance.</returns>
public static TBuilder With<TBuilder, TField>(this TBuilder @this, ref List<TField> field, IEnumerable<TField> values)
where TBuilder : IBuilder
{
if (field is null)
{
throw new ArgumentNullException(nameof(field));
}
if (values is null)
{
field = null;
}
else
{
field.AddRange(values);
}
return @this;
}
/// <summary>
/// Sets a list field to the specified value.
/// It will add the value to the specified list, and won't override existing values.
/// </summary>
/// <typeparam name="TBuilder">The type of builder.</typeparam>
/// <typeparam name="TField">The type of field.</typeparam>
/// <param name="this">The builder instance to use.</param>
/// <param name="field">The reference to the list field we are setting.</param>
/// <param name="value">The new value to add to the list field.</param>
/// <returns>The builder instance.</returns>
public static TBuilder With<TBuilder, TField>(this TBuilder @this, ref List<TField> field, TField value)
where TBuilder : IBuilder
{
if (field is null)
{
throw new ArgumentNullException(nameof(field));
}
field.Add(value);
return @this;
}
}
}