11

In my Roslyn analyzer I get Microsoft.CodeAnalysis.TypeInfo of an argument by

var argumentTypeInfo = semanticModel.GetTypeInfo(argumentSyntax.Expression);

also I have another instance of Microsoft.CodeAnalysis.TypeInfo called targetTypeInfo. Now I want to know if the type describled by targetTypeInfo is assignable from the type describled by argumentTypeInfo.

I know System.Reflection.TypeInfo has its IsAssignableFrom method, which is exactly what I want. But the convertion between the two TypeInfos is a problem. What's the correct way to get type relationships in an analyzer?

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • 5
    You can check the base classes and implemented interfaces of the `ITypeSymbol` in `TypeInfo` in question. But this might be cumbersome. Another approach which might work is to use the `Compilation.ClassifyConversion` and check the returned `ConversionKind`. A third option is to check how Roslyn binds the `is` operator: http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp/Binder/Binder_Expressions.cs,507, but I guess it uses the `ClassifyConversion`. Let me know if any of these worked. – Tamas Feb 24 '17 at 09:08

0 Answers0