繁体   English   中英

属性的Roslyn检查类型

[英]Roslyn Check Type of an Attribute

我试图找出一种适当的方法来比较罗斯林中的属性数据。

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运行时类型。 您可以只比较类型名称等,以查看它们是否可能是相同的类型,也可以为MyAttributeType获取Roslyn类型符号,这更正确。 这通常是通过

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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