简体   繁体   中英

Roslyn Check Type of an Attribute

I'm trying to figure out there proper way to compare attribute data in Roslyn.

static bool ResolveAttributes(IMethodSymbol methodSymbol)
{
    var attributes = methodSymbol.GetAttributes();

    return null == attributes.FirstOrDefault(attr => isIDEMessageAttribute(attr, typeof(MyAttributeType)));
}

static bool IsIDEMessageAttribute(AttributeData attribute, Type desiredAttributeType)
{
    //How can I check if the attribute is the type of desired?
}

How can I check if the attribute is the type of desired?

AttributeData.AttributeClass gives you the Roslyn symbol for the attribute. But you've got a CLR runtime Type you are trying to compare against. You can either just compare the type names, etc, to see if they are the probably the same type, or get a Roslyn type symbol for MyAttributeType , which is more correct. This is typically done via

var desiredSymbol = sematicModel.Compilation.GetTypeByMetadataName(typeof(MyAttributeType).FullName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM