[英]How do i get the expected type in an expression using roslyn?
我想为 CS0417 开发一个代码修复,并且一直在寻找如何推断修复重载解析错误所需的类型。
从来源来看,可能找出需要哪种类型所需的代码目前是内部的。 有没有人知道一种内置的方法来确定以下代码中需要“字符串”?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
TypeName()
{
var action = new Func<int, string>(TestMethod);
}
private int TestMethod(int arg)
{
throw new NotImplementedException();
}
}
}
可以拉取这个分支,调试测试方法Cs0407FixGenerateFixedReturn
,快速上手。
如果类型是委托,则解决方案相当简单,这符合我的需要:
var creationSyntax = argumentSyntax.AncestorsAndSelf().OfType<ObjectCreationExpressionSyntax>().FirstOrDefault();
if (creationSyntax == null)
return false;
var symbolInfo = semanticModel.GetSymbolInfo(creationSyntax.Type).Symbol;
if (symbolInfo is INamedTypeSymbol namedType)
{
symbol = namedType?.DelegateInvokeMethod?.ReturnType;
return symbol != null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.