C#C
C#4y ago
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)

I thought I'd be able to do this:
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

How do I check a type against another type? Google is only helping me with checking an object against a type.
Was this page helpful?