[英]Roslyn: How do I tell if a particular assembly is referenced in a Compilation?
I'm writing a Roslyn diagnostic analyzer. 我正在写一个Roslyn诊断分析仪。 I want to tell if
System.Collections.Immutable
is referenced in RegisterCompilationStartAction
before I register any other actions. 我想告诉我在注册任何其他操作之前是否在
RegisterCompilationStartAction
引用System.Collections.Immutable
。 This is the way I'm going about it so far: 这是我到目前为止的方式:
context.RegisterCompilationStartAction(compilationStartContext =>
{
var compilation = compilationStartContext.Compilation;
if (compilation.GetTypeByMetadataName("System.Collections.Immutable.ImmutableArray`1") == null)
{
return;
}
...
});
This works, but I don't feel it's the cleanest way to do this. 这有效,但我觉得这不是最干净的方法。 Can I somehow get a
MetadataReference
corresponding to the assembly name instead and check if it's null, like GetMetadataReference("System.Collections.Immutable") == null
? 我可以以某种方式获取与程序集名称对应的
MetadataReference
,并检查它是否为null,如GetMetadataReference("System.Collections.Immutable") == null
? ( GetMetadataReference
doesn't accept a string, so that doesn't actually work.) If not, any other cleaner way to do this that doesn't involve picking out a particular type? (
GetMetadataReference
不接受字符串,因此实际上不起作用。)如果没有,任何其他更简洁的方法来执行此操作不涉及选择特定类型? Thanks. 谢谢。
Instead of searching for the type you could simply search through the References and resolve the MetadataReference to check whether a specific assembly is included within a Project: 您可以直接搜索引用并解析MetadataReference以检查项目中是否包含特定程序集,而不是搜索类型:
if(!compilation.References.Any(reference =>
compilation.GetAssemblyOrModuleSymbol(reference)
.Name == "System.Collections.Immutable"))
{
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.