I'm writing a Roslyn diagnostic analyzer. I want to tell if System.Collections.Immutable
is referenced in RegisterCompilationStartAction
before I register any other actions. 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
? ( 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? 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:
if(!compilation.References.Any(reference =>
compilation.GetAssemblyOrModuleSymbol(reference)
.Name == "System.Collections.Immutable"))
{
return;
}
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.