/
CustomFilteringAttribute.cs
58 lines (50 loc) · 1.72 KB
/
CustomFilteringAttribute.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
using System.Diagnostics.CodeAnalysis;
namespace Serenity.ComponentModel;
/// <summary>
/// Base class which other custom filtering types derive from.
/// </summary>
/// <seealso cref="FilteringTypeAttribute" />
/// <remarks>
/// Initializes a new instance of the <see cref="CustomFilteringAttribute"/> class.
/// </remarks>
/// <param name="filteringType">Type of the filtering.</param>
public abstract class CustomFilteringAttribute(string filteringType) : FilteringTypeAttribute(filteringType)
{
private Dictionary<string, object?>? options;
/// <summary>
/// Sets the parameters.
/// </summary>
/// <param name="filteringParams">The filtering parameters.</param>
public override void SetParams(IDictionary<string, object?> filteringParams)
{
if (filteringParams is null)
throw new ArgumentNullException(nameof(filteringParams));
if (options != null)
foreach (var opt in options)
filteringParams[opt.Key] = opt.Value;
}
/// <summary>
/// Sets the option.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
protected void SetOption(string key, object? value)
{
options ??= new(StringComparer.Ordinal);
options[key] = value;
}
/// <summary>
/// Gets the option.
/// </summary>
/// <typeparam name="TType">The type of the value.</typeparam>
/// <param name="key">The key.</param>
[return:MaybeNull]
protected TType GetOption<TType>(string key)
{
if (options == null)
return default;
if (!options.TryGetValue(key, out object? obj) || obj == null)
return default;
return (TType)obj;
}
}