我创建了以下对象来构建我的构造函数:
internal class ConstructorWalker : CSharpSyntaxWalker
{
private string className = String.Empty;
private readonly SemanticModel semanticModel;
private readonly Action<string> callback;
public ConstructorWalker(Document document, Action<string> callback)
{
this.semanticModel = document.GetSemanticModelAsync().Result;
this.callback = callback;
}
public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
{
var typeToMatch = typeof(Dictionary<string, Func<GenericMobileRequest, Result<object>, Task>>);
var parameters = node.ParameterList;
foreach (var param in parameters.ChildNodes()) {
//This does not work... .Symbol is null
var paramType = ((IParameterSymbol)semanticModel.GetSymbolInfo(param).Symbol).Type;
if(paramType == typeToMatch) {
//PROFIT!!!
}
}
如何确定参数的类型,以确保它属于我感兴趣的类型?
答案 0 :(得分:0)
使用Roslyn无法轻松获取参数的实际Type
。您可以获得TypeSyntax
和ITypeSymbol
,如下所示,但除非您使用反射,否则您无法真正获得Type
个对象(据我所知)。
string typeToMatchString = "Dictionary<string, Func<Exception, HashSet<object>, Task>>"
foreach (var parameter in node.ParameterList.Parameters)
{
var typeSyntax = parameter.Type;
var typeSymbol = semanticModel.GetTypeInfo(typeSyntax).Type;
// Maybe comparing the name is enough?
if (typeSymbol.ToDisplayString() == typeToMatchString)
//PROFIT???
}
您可能还想查看this related question。