属性的Roslyn检查类型 [英] Roslyn Check Type of an Attribute

查看:74
本文介绍了属性的Roslyn检查类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出在罗斯林中比较属性数据的正确方法.

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为您提供该属性的Roslyn符号.但是您有一个要与之进行比较的CLR运行时类型.您可以只比较类型名称等,以查看它们是否可能是相同的类型,或者获取MyAttributeType的Roslyn类型符号,这更正确.这通常是通过以下方式完成的

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)

这篇关于属性的Roslyn检查类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆