Skip to content

Custom Attributes in .NET: retarget to net10.0, add the reading-API examples - #2294

Merged
vladimir-pecanac-main merged 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/61252-dotnet-custom-attributes
Oct 14, 2026
Merged

vladimir-pecanac-main merged 3 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/61252-dotnet-custom-attributes

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the sample for the article "Custom Attributes in .NET" (https://code-maze.com/dotnet-custom-attributes/).

Only aspnetcore-features/CustomAttributes changes.

  • Both projects move from net6.0 to net10.0.
  • Test packages, re-queried on NuGet 2026-10-11 (latest stable): Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter 4.5.1, MSTest.TestFramework 4.5.1, coverlet.collector 10.1.0. The project keeps the MSTest.TestAdapter + MSTest.TestFramework pair it already used.
  • GetAttribute: the "atributes" typo in the not-found message is fixed.
  • GetAttributesOfMethods: Type.GetMethods returns an empty array, never null, so the guard checks Length only.
  • The unused Visual Studio using headers are gone, every file uses a file-scoped namespace, and the list is declared with var.
  • New in CustomAttributeHelper: GetAttributeGeneric (the GetCustomAttribute extension method), HasAttribute (IsDefined) and CountInheritedAttributes, which reads an inherited attribute through Attribute.GetCustomAttributes and through MemberInfo.GetCustomAttributes.
  • TaskBoard and TeamTaskBoard carry an inherited [Reviewed] attribute on a virtual property and a virtual method. The test shows the static method finds it on both, while the instance method ignores inherit on the property only.
  • Three new tests cover the new helpers. No test name contains Live.

Local run on SDK 10.0.302, runtime 10.0.10: build 0 warnings 0 errors, dotnet test Passed 6 Failed 0, dotnet list package --vulnerable --include-transitive clean on both projects. Program.cs is unchanged.

- CustomAttributes and TestProject move from net6.0 to net10.0.
- Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 4.5.1,
  coverlet.collector 10.1.0.
- Fix the "atributes" typo in GetAttribute's message.
- GetMethods never returns null, so the guard checks Length only.
- Drop the unused using headers, use file-scoped namespaces and var.
- Add GetAttributeGeneric<T> (GetCustomAttribute<T>), HasAttribute<T> (IsDefined) and
  CountInheritedAttributes<T>, which reads an inherited attribute through
  Attribute.GetCustomAttributes and through MemberInfo.GetCustomAttributes. TaskBoard and
  TeamTaskBoard show that the instance method ignores inherit on a property but not on a method.
- Three tests cover the new helpers.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 026cf6d into CodeMazeBlog:main Oct 14, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant