Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AttributeUtilities.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
4
6
7public static class AttributeUtilities
8{
9 private static class TypeAttributeCache<T, A> where A : Attribute
10 {
11 public static readonly A Value = typeof(T).GetAttribute<A>();
12 }
13
14 public static T GetAttribute<T>(this MethodBase method) where T : Attribute
15 {
16 return (T)method.GetCustomAttributes(typeof(T), inherit: false).SingleOrDefault();
17 }
18
19 public static T GetAttribute<T>(this Enum value) where T : Attribute
20 {
22 string name = Enum.GetName(type, value);
23 return type.GetField(name).GetCustomAttributes(inherit: false).OfType<T>()
24 .SingleOrDefault();
25 }
26
27 public static A GetCacheableAttribute<T, A>() where A : Attribute
28 {
30 }
31
32 public static T GetAttribute<T>(this Type type) where T : Attribute
33 {
34 return type.GetCustomAttributes(inherit: false).OfType<T>().SingleOrDefault();
35 }
36}
static T GetAttribute< T >(this MethodBase method)
static Attribute[] GetCustomAttributes(MemberInfo element, Type attributeType)
Definition Attribute.cs:338
static ? string GetName(Type enumType, object value)
Definition Enum.cs:281
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408