|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Reflection; |
5 | | -using System.Text; |
6 | | -using System.Threading.Tasks; |
7 | | - |
8 | | -namespace CustomAttributes |
| 1 | +using System.Reflection; |
| 2 | + |
| 3 | +namespace CustomAttributes; |
| 4 | + |
| 5 | +public static class CustomAttributeHelper |
9 | 6 | { |
10 | | - public static class CustomAttributeHelper |
| 7 | + private static void WriteOnTheConsole(Attribute attributeInstance) |
11 | 8 | { |
12 | | - private static void WriteOnTheConsole(Attribute attributeInstance) |
13 | | - { |
14 | | - var pInfoList = attributeInstance.GetType().GetProperties(BindingFlags.Public | |
15 | | - BindingFlags.Instance | |
16 | | - BindingFlags.DeclaredOnly); |
| 9 | + var pInfoList = attributeInstance.GetType().GetProperties(BindingFlags.Public | |
| 10 | + BindingFlags.Instance | |
| 11 | + BindingFlags.DeclaredOnly); |
17 | 12 |
|
18 | | - Console.WriteLine($"\nThe {attributeInstance} attribute:"); |
| 13 | + Console.WriteLine($"\nThe {attributeInstance} attribute:"); |
19 | 14 |
|
20 | | - foreach (var pInfo in pInfoList) |
21 | | - Console.WriteLine($"The {pInfo.Name} property is: {pInfo.GetValue(attributeInstance)}"); |
22 | | - } |
| 15 | + foreach (var pInfo in pInfoList) |
| 16 | + Console.WriteLine($"The {pInfo.Name} property is: {pInfo.GetValue(attributeInstance)}"); |
| 17 | + } |
23 | 18 |
|
24 | | - public static string? GetAttribute(Type desiredType, Type desiredAttribute) |
25 | | - { |
26 | | - var attributeInstance = Attribute.GetCustomAttribute(desiredType, desiredAttribute); |
| 19 | + public static string? GetAttribute(Type desiredType, Type desiredAttribute) |
| 20 | + { |
| 21 | + var attributeInstance = Attribute.GetCustomAttribute(desiredType, desiredAttribute); |
27 | 22 |
|
28 | | - if (attributeInstance == null) |
29 | | - Console.WriteLine($"The class {desiredType} does not have atributes."); |
30 | | - else |
31 | | - WriteOnTheConsole(attributeInstance); |
| 23 | + if (attributeInstance == null) |
| 24 | + Console.WriteLine($"The class {desiredType} does not have attributes."); |
| 25 | + else |
| 26 | + WriteOnTheConsole(attributeInstance); |
32 | 27 |
|
33 | | - return attributeInstance?.ToString(); |
34 | | - } |
| 28 | + return attributeInstance?.ToString(); |
| 29 | + } |
| 30 | + |
| 31 | + public static T? GetAttributeGeneric<T>(Type desiredType) where T : Attribute |
| 32 | + { |
| 33 | + return desiredType.GetCustomAttribute<T>(); |
| 34 | + } |
| 35 | + |
| 36 | + public static bool HasAttribute<T>(Type desiredType) where T : Attribute |
| 37 | + { |
| 38 | + return desiredType.IsDefined(typeof(T)); |
| 39 | + } |
35 | 40 |
|
36 | | - public static List<string> GetAttributesOfMethods(Type elementType) |
| 41 | + public static List<string> GetAttributesOfMethods(Type elementType) |
| 42 | + { |
| 43 | + var attributes = new List<string>(); |
| 44 | + |
| 45 | + var methodInfoList = elementType.GetMethods(BindingFlags.Public | |
| 46 | + BindingFlags.Instance | |
| 47 | + BindingFlags.DeclaredOnly); |
| 48 | + |
| 49 | + if (methodInfoList.Length == 0) |
37 | 50 | { |
38 | | - List<string> attributes = new List<string>(); |
| 51 | + Console.WriteLine($"The type {elementType} does not have any methods."); |
| 52 | + return attributes; |
| 53 | + } |
39 | 54 |
|
40 | | - var methodInfoList = elementType.GetMethods(BindingFlags.Public | |
41 | | - BindingFlags.Instance | |
42 | | - BindingFlags.DeclaredOnly); |
| 55 | + foreach (var methodInfo in methodInfoList) |
| 56 | + { |
| 57 | + var attributeList = Attribute.GetCustomAttributes(methodInfo, true); |
43 | 58 |
|
44 | | - if (methodInfoList == null || methodInfoList.Length == 0) |
| 59 | + if (attributeList.Length == 0) |
45 | 60 | { |
46 | | - Console.WriteLine($"The type {elementType} does not have any methods."); |
47 | | - return attributes; |
| 61 | + Console.WriteLine($"The {elementType.Name}.{methodInfo.Name} method does not have attributes."); |
| 62 | + continue; |
48 | 63 | } |
49 | 64 |
|
50 | | - foreach (var methodInfo in methodInfoList) |
51 | | - { |
52 | | - var attributeList = Attribute.GetCustomAttributes(methodInfo, true); |
| 65 | + Console.WriteLine($"The {elementType.Name}.{methodInfo.Name} method's attribute:"); |
53 | 66 |
|
54 | | - if (attributeList.Length == 0) |
55 | | - { |
56 | | - Console.WriteLine($"The {elementType.Name}.{methodInfo.Name} method does not have attributes."); |
57 | | - continue; |
58 | | - } |
| 67 | + foreach (var att in attributeList) |
| 68 | + { |
| 69 | + WriteOnTheConsole(att); |
| 70 | + attributes.Add(methodInfo.Name + "-" + att.ToString()); |
| 71 | + } |
59 | 72 |
|
60 | | - Console.WriteLine($"The {elementType.Name}.{methodInfo.Name} method's attribute:"); |
| 73 | + Console.WriteLine(); |
| 74 | + } |
61 | 75 |
|
62 | | - foreach (var att in attributeList) |
63 | | - { |
64 | | - WriteOnTheConsole(att); |
65 | | - attributes.Add(methodInfo.Name + "-" + att.ToString()); |
66 | | - } |
| 76 | + return attributes; |
| 77 | + } |
67 | 78 |
|
68 | | - Console.WriteLine(); |
69 | | - } |
| 79 | + public static (int FromAttributeClass, int FromMember) CountInheritedAttributes<T>(MemberInfo member) |
| 80 | + where T : Attribute |
| 81 | + { |
| 82 | + var fromAttributeClass = Attribute.GetCustomAttributes(member, true).OfType<T>().Count(); |
| 83 | + var fromMember = member.GetCustomAttributes(true).OfType<T>().Count(); |
70 | 84 |
|
71 | | - return attributes; |
72 | | - } |
| 85 | + return (fromAttributeClass, fromMember); |
73 | 86 | } |
74 | 87 | } |
0 commit comments