Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Generic Attribute Losing Its Type Argument at Runtime

- 25.11.26 - ErcanOPAK

🔧 C#: Fix a Generic Attribute Losing Its Type Argument at Runtime

C# 11’s generic attributes let you write [Validates<Foo>], but reflecting on them at runtime means the applied attribute’s actual type is the closed generic ValidatesAttribute<Foo>, not the open generic definition — calling a non-generic lookup method looking for the open definition finds nothing at all, returning null even though the attribute is clearly visible in source and IntelliSense, because the lookup was never looking for the right closed type to begin with.

🔎 The Problem

public class ValidatesAttribute<T> : Attribute { }

[Validates<CustomerDto>]
public class CustomerValidator { }

var attr = typeof(CustomerValidator)
    .GetCustomAttribute<ValidatesAttribute>();  // wrong: no type argument
// attr is null - ValidatesAttribute (open) never matches
// ValidatesAttribute<CustomerDto> (the actual applied, closed type)

✅ Fix: Match on the Open Generic Definition, or the Exact Closed Type

  • When the type argument isn’t known ahead of time, use GetCustomAttributes(inherit: true) and filter by checking attr.GetType().IsGenericType && attr.GetType().GetGenericTypeDefinition() == typeof(ValidatesAttribute<>), rather than calling a generic method with an unknown type argument.
  • If the specific closed type is known at the call site, use GetCustomAttribute<ValidatesAttribute<CustomerDto>>() with the exact same type argument used at the application site — the open generic definition will never match a closed instance.
  • Consider having the generic attribute implement a shared non-generic marker interface, or expose its type argument as a Type property, specifically so reflection code that doesn’t know it ahead of time has a non-generic way to find and inspect it.

🚨 Why This Is Dangerous

  • Validation, serialization, or DI registration logic that silently finds “no attribute” because of a reflection lookup mismatch doesn’t throw — it just skips whatever behavior the attribute was supposed to trigger, which can look like the feature was never implemented rather than a lookup bug.

[Validates<Foo>] is not an instance of ValidatesAttribute at runtime — it’s an instance of ValidatesAttribute<Foo>, and reflection only finds what you actually ask for.

— C# Best Practice

Related posts:

C#: Fix a CancellationTokenSource.CancelAfter Timer That Never Fires Because the Source Was Disposed...

C#: Fix a CancellationTokenSource.CancelAfter Timer That Never Fires Because the Source Was Disposed...

ASP.NET Core Middleware Order — Why Your App Breaks for No Reason

ASP.NET Core Middleware Order — Why Your App Breaks for No Reason

C#: Why an Event Handler Keeps a Disposed Object Alive and Leaks Memory

C#: Why an Event Handler Keeps a Disposed Object Alive and Leaks Memory

Views: 13
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Git: Fix an Alias Expanding Differently Inside a CI Runner
Photoshop: Fix a Color Profile Mismatch Shifting an Exported JPEG

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com