在我的 Roslyn 分析器中,我通过
得到了
Microsoft.CodeAnalysis.TypeInfo
的参数
var argumentTypeInfo = semanticModel.GetTypeInfo(argumentSyntax.Expression);
我还有另一个
Microsoft.CodeAnalysis.TypeInfo
的实例,称为 targetTypeInfo
。现在我想知道 targetTypeInfo
描述的类型是否可以从 argumentTypeInfo
描述的类型分配。
我知道
System.Reflection.TypeInfo
有它的 IsAssignableFrom
方法,这正是我想要的。但两个TypeInfo
之间的转换是个问题。在分析器中获取类型关系的正确方法是什么?
塔马斯的评论很有帮助。它朝着我找到的解决方案的方向发展。
现在可以使用
Compilation.HasImplicitConversion(ITypeSymbol? fromType, ITypeSymbol? toType)
方法。
此处讨论了 https://github.com/dotnet/roslyn/issues/9461,然后添加了 PR #26719。
这正是我发现这个问题时想要的。