Skip to content

Commit af88c4f

Browse files
Merge branch 'main' into seo/96092-csharp-volatile-interlocked-lock
2 parents 2dd4df7 + 743ed21 commit af88c4f

98 files changed

Lines changed: 2259 additions & 460 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,87 @@
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
96
{
10-
public static class CustomAttributeHelper
7+
private static void WriteOnTheConsole(Attribute attributeInstance)
118
{
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);
1712

18-
Console.WriteLine($"\nThe {attributeInstance} attribute:");
13+
Console.WriteLine($"\nThe {attributeInstance} attribute:");
1914

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+
}
2318

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);
2722

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);
3227

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+
}
3540

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)
3750
{
38-
List<string> attributes = new List<string>();
51+
Console.WriteLine($"The type {elementType} does not have any methods.");
52+
return attributes;
53+
}
3954

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);
4358

44-
if (methodInfoList == null || methodInfoList.Length == 0)
59+
if (attributeList.Length == 0)
4560
{
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;
4863
}
4964

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:");
5366

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+
}
5972

60-
Console.WriteLine($"The {elementType.Name}.{methodInfo.Name} method's attribute:");
73+
Console.WriteLine();
74+
}
6175

62-
foreach (var att in attributeList)
63-
{
64-
WriteOnTheConsole(att);
65-
attributes.Add(methodInfo.Name + "-" + att.ToString());
66-
}
76+
return attributes;
77+
}
6778

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();
7084

71-
return attributes;
72-
}
85+
return (fromAttributeClass, fromMember);
7386
}
7487
}

‎aspnetcore-features/CustomAttributes/CustomAttributes/CustomAttributes.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace CustomAttributes;
62

7-
namespace CustomAttributes
3+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
4+
public class DeveloperTaskAttribute : Attribute
85
{
9-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
10-
public class DeveloperTaskAttribute : Attribute
6+
public DeveloperTaskAttribute(Priorities priority)
117
{
12-
public DeveloperTaskAttribute(Priorities priority)
13-
{
14-
Priority = priority;
15-
}
16-
17-
public Priorities Priority { get; set; }
18-
public string? Description { get; set; }
8+
Priority = priority;
199
}
10+
11+
public Priorities Priority { get; set; }
12+
public string? Description { get; set; }
2013
}
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace CustomAttributes;
62

7-
namespace CustomAttributes
3+
public enum Priorities
84
{
9-
public enum Priorities
10-
{
11-
High,
12-
Mid,
13-
Low
14-
}
5+
High,
6+
Mid,
7+
Low
158
}
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace CustomAttributes;
62

7-
namespace CustomAttributes
3+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
4+
public class ManagerTaskAttribute : Attribute
85
{
9-
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
10-
public class ManagerTaskAttribute : Attribute
11-
{
12-
public Priorities Priority { get; set; }
13-
public bool NeedsReport { get; set; }
14-
}
6+
public Priorities Priority { get; set; }
7+
public bool NeedsReport { get; set; }
158
}
Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace CustomAttributes;
62

7-
namespace CustomAttributes
3+
[TaskDescriptor(Name = "The task's name",
4+
Description = "Some descriptions for the task",
5+
NeedsManager = true,
6+
DeveloperCount = 5)]
7+
public class MyTasks
88
{
9-
[TaskDescriptor(Name = "The task's name",
10-
Description = "Some descriptions for the task",
11-
NeedsManager = true,
12-
DeveloperCount = 5)]
13-
public class MyTasks
9+
[DeveloperTask(Priorities.Low)]
10+
[DeveloperTask(Priorities.High, Description = "High level description")]
11+
public void ScheduleMeeting()
1412
{
15-
[DeveloperTask(Priorities.Low)]
16-
[DeveloperTask(Priorities.High, Description = "High level description")]
17-
public void ScheduleMeeting()
18-
{
19-
}
13+
}
2014

21-
[ManagerTask(Priority = Priorities.Mid, NeedsReport = true)]
22-
[DeveloperTask(Priorities.High, Description = "High level description")]
23-
public virtual void ScheduleInterview()
24-
{
25-
}
15+
[ManagerTask(Priority = Priorities.Mid, NeedsReport = true)]
16+
[DeveloperTask(Priorities.High, Description = "High level description")]
17+
public virtual void ScheduleInterview()
18+
{
2619
}
2720
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace CustomAttributes;
2+
3+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
4+
public class ReviewedAttribute : Attribute
5+
{
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace CustomAttributes;
2+
3+
public class TaskBoard
4+
{
5+
[Reviewed]
6+
public virtual int OpenTasks { get; set; }
7+
8+
[Reviewed]
9+
public virtual void Archive()
10+
{
11+
}
12+
}
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace CustomAttributes;
62

7-
namespace CustomAttributes
3+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
4+
public class TaskDescriptorAttribute : Attribute
85
{
9-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
10-
public class TaskDescriptorAttribute : Attribute
11-
{
12-
public string? Name { get; set; }
13-
public string? Description { get; set; }
14-
public bool NeedsManager { get; set; }
15-
public int DeveloperCount { get; set; }
16-
}
6+
public string? Name { get; set; }
7+
public string? Description { get; set; }
8+
public bool NeedsManager { get; set; }
9+
public int DeveloperCount { get; set; }
1710
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CustomAttributes;
2+
3+
public class TeamTaskBoard : TaskBoard
4+
{
5+
public override int OpenTasks { get; set; }
6+
7+
public override void Archive()
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)