© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
9 replies
Hugh

Comparing types when using reflection

I'm using the reflection system, and want to check the type of an attribute (this may not be relevant here, but I wanted to give some context)

I'm iterating over method custom attributes, and want to check if one of them is a specific type (
MyAttribute
MyAttribute
)

I thought I'd be able to do this:
foreach(var customAttr in method.CustomAttributes)
{
    if(customAttr.AttributeType is MyAttribute)
foreach(var customAttr in method.CustomAttributes)
{
    if(customAttr.AttributeType is MyAttribute)


However, it never hits this, and VSCode underlines it in yellow saying
The given expression is never of the provided ('MyAttribute') type
The given expression is never of the provided ('MyAttribute') type


How do I check a type against another type? Google is only helping me with checking an object against a type.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Comparing Types using local var Type type
C#CC# / help
3y ago
❔ Expression<Func> using Reflection
C#CC# / help
3y ago
❔ System.Reflection.ReflectionTypeLoadException: „Unable to load one or more of the requested types.
C#CC# / help
3y ago
Accessing private methods when using Reflection.Emit and AssemblyBuilder
C#CC# / help
2y ago