I have been working on some Roslyn code analyzer. My goal is to analyze exceptions and Catch
blocks.
I got to a situation when I am able to analyze nodes and get NamedType
of every Exception
possibly thrown by the node. At the same time I am able to enumerate all Catch
blocks related to such a node.
I need to resolve whether given NamedType
is equal to Catch
expression declaration or its base class.
Illustrative code:
var typeName = "System.ArgumentNullException";
NamedType exceptionType = _compilatorStartContext.Compilation.GetTypeByMetadataName(typeName);
// exceptionType = NamedType System.IO.IOException
var tryStatement = arg as TryStatementSyntax;
CatchDeclarationSyntax @catch = tryStatement.Catches.First();
var declaration = @catch.Declaration;
// declaration = CatchDeclarationSyntax CatchDeclaration (Exception)
// TODO: decide whether declaration can be instantiated from exceptionType