forked from KyleBanks/scene-ref-attribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionUtil.cs
51 lines (47 loc) · 1.58 KB
/
ReflectionUtil.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
using System;
using System.Collections.Generic;
using System.Reflection;
namespace KBCore.Refs
{
internal static class ReflectionUtil
{
internal struct AttributedField<T>
where T : Attribute
{
public T Attribute;
public FieldInfo FieldInfo;
}
internal static void GetFieldsWithAttributeFromType<T>(
Type classToInspect,
IList<AttributedField<T>> output,
BindingFlags reflectionFlags = BindingFlags.Default
)
where T : Attribute
{
Type type = typeof(T);
do
{
FieldInfo[] allFields = classToInspect.GetFields(reflectionFlags);
for (int f = 0; f < allFields.Length; f++)
{
FieldInfo fieldInfo = allFields[f];
Attribute[] attributes = Attribute.GetCustomAttributes(fieldInfo);
for (int a = 0; a < attributes.Length; a++)
{
Attribute attribute = attributes[a];
if (!type.IsInstanceOfType(attribute))
continue;
output.Add(new AttributedField<T>
{
Attribute = attribute as T,
FieldInfo = fieldInfo
});
break;
}
}
classToInspect = classToInspect.BaseType;
}
while (classToInspect != null);
}
}
}