-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathApplyFiltersAttribute.cs
More file actions
40 lines (31 loc) · 923 Bytes
/
ApplyFiltersAttribute.cs
File metadata and controls
40 lines (31 loc) · 923 Bytes
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
using PostSharp.Aspects;
using PostSharp.Serialization;
using System;
namespace PostSharp.Samples.Encryption
{
[PSerializable]
[AttributeUsage(AttributeTargets.Parameter)]
[LinesOfCodeAvoided(2)]
public class ApplyFiltersAttribute : FilterAttribute
{
// TODO: You may want to consider a design when Filter does not apply the filter on the current instance but clones the object and filters the clone.
public override object ApplyFilter(object value)
{
if (value == null)
{
return null;
}
GetFilterable(value).ApplyFilter();
return value;
}
private static IFilterable GetFilterable(object value)
{
var filterable = value as IFilterable;
if (filterable == null)
{
throw new InvalidOperationException($"The type {value.GetType().FullName} is not IFilterable.");
}
return filterable;
}
}
}