-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAttributeHelper.cs
More file actions
31 lines (24 loc) · 1.19 KB
/
AttributeHelper.cs
File metadata and controls
31 lines (24 loc) · 1.19 KB
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
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Dat.FileParsing;
public static class AttributeHelper
{
//public static T? Get<T>(Type t) where T : Attribute
//{
// var attributes = t.GetCustomAttributes(typeof(T), inherit: false);
// return attributes.Length == 1 ? attributes[0] as T : null;
//}
public static T? Get<T>(PropertyInfo p) where T : Attribute
{
var attributes = p.GetCustomAttributes(typeof(T), inherit: false);
return attributes.Length == 1 ? attributes[0] as T : null;
}
public static T? Get<T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t) where T : Attribute
=> t.GetCustomAttribute<T>(inherit: false);
public static bool Has<T>(PropertyInfo p) where T : Attribute
=> p.GetCustomAttribute<T>(inherit: false) is not null;
public static bool Has<T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t) where T : Attribute
=> t.GetCustomAttribute<T>(inherit: false) is not null;
public static IEnumerable<PropertyInfo> GetAllPropertiesWithAttribute<T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type t) where T : Attribute
=> t.GetProperties().Where(Has<T>);
}