我试图找出比较 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?
}
如何检查属性是否是所需的类型?
AttributeData.AttributeClass
为您提供该属性的 Roslyn 符号。但是您有一个要与之比较的 CLR 运行时类型。您可以只比较类型名称等,看看它们是否可能是相同的类型,或者获取 Roslyn 类型符号MyAttributeType
,这是更正确的。这通常是通过以下方式完成的
var desiredSymbol = sematicModel.Compilation.GetTypeByMetadataName(typeof(MyAttributeType).FullName)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)