Derive parameter type with Roslyn
我创建了以下对象来遍历我的构造函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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!!! } } |
如何确定参数的类型以确保它是我感兴趣的类型?
使用 Roslyn 无法如此轻松地获取参数的实际
1 2 3 4 5 6 7 8 9 10 11 | 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??? } |
您可能也想查看这个相关问题。